简体   繁体   中英

How do I create an object class with object properties in JavaScript?

I am writing a music related program and want to have objects as properties within my objects. I know it is possible to do one by one, but I would like a shortcut. This is what I want to do, and I know does not work. What is the correct way? Is it possible?

function OctaveNote(note, value) {
    this.firstOctave = note + 1.value = value;
    this.secondOctave = note + 2.value = value + 12;
    this.thirdOctave = note + 3.value = value + 24;
}

Or

function OctaveNote(note, value) {    
    this.firstOctave = note + 1;
    this.firstOctave.value = value;
    this.secondOctave = note + 2;
    this.secondOctave.value = value + 12;
    this.thirdOctave = note + 3;
    this.thirdOctave.value = value + 24;
}

So that C = new OctaveNote ("C", 0); let me know that C3.value = 24 and I don't have to write individual objects for each octave of all 11 notes, 99 lines!

Yes but it need to be objects not a string.

This create a string : this.firstOctave = note + 1;

But you can't add the propetry value to a string.

So what you need to do is create an object like :

// Constructor 
function OctaveNote(note, value) {
    // If we have a note and a value, we add a note.
    if (typeof note !== 'undefined' && typeof value !== 'undefined') this.addNote(note, value);  
}

OctaveNote.prototype.addNote = function(note, value) {
    this[note+1] = value;
    this[note+2] = value + 12;
    this[note+3] = value + 24;
}

var octave =  new OctaveNote("B", 14);
octave.addNote('C', 2);
octave.addNote('A', 6);
console.log(octave.C1); // 2
console.log(octave.A2); // 18
console.log(octave.C3); // 26

jsFiddle

Your second example should be one that covers what you need. What you have wrong is the usage/how to call it.

When you create your C object with

C = new OctaveNote("C", 0)

you now have an instance of OctaveNote that you can access with all the properties that were set in the constructor function.

So you can get the thirdOctave by calling

C.thirdOctave.value which should return 24 .

Your problem here is that thirdOctave itself is not an object and so it can't hold properties, like value. You could either convert thirdOctave to an object containing a string and value pair, or you could simply store your value in its own separate property: thirdOctaveValue .

So you could transform the function into something like:

function OctaveNote(note, value) {
    this.firstOctaveName = note + 1;
    this.firstOctaveValue = value;
    this.secondOctaveName = note + 2;
    this.secondOctaveValue = value + 12;
    this.thirdOctaveName = note + 3;
    this.thirdOctaveValue = value + 24;
}

You can then initiate objects for each note:

D = new OctaveNote("D", 20);
X = new OctaveNote("X", 32);

And get the values out of them:

console.log(D.firstOctaveValue);
console.log(X.secondOctaveValue);

etc

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM