2

for the sake of fun and exploring nodeJS I made an object

var f15c = {
fuel : 10000,
IRST: true,
AESA: true,
speed: 2500,
Ammo:
 {
    AMRAAM: 6,
    Python5: 2,
    Delilah: 3,
 },
tailnumber : the question begins here.
}

The problem came when I wanted to add tailnumber that is not the same for every plane but should be assigned to the plane. what is the methode?

var f15c = {
...
tailnumber = function (tn){"whats should i add here?"}
}

or

var f15c = {
...
tailnumber: ?? what should i place here?
SetTailNumber = function(tn)
 {
   tailnumber=tn;
 }
}

or must I have the entire F15c as a function?

var f15c = function(tn) {
...
tailnumber = tn;

but then i cannot set the entire variables complex.

or perhaps I'm doing it wrong and should refer the variable as an individual F15 and use a different function to create it? but then how do I make that field in a way it is unassigned and waiting to be assigned (and then saves the assigned number)?

would appreciate some heads up

2
  • What exactly are you trying to do? It is much more important question than the "how to use function". To what you are doing - it looks like that define class and pass tailnumber into the constructor is much better option. Commented Dec 20, 2017 at 16:01
  • You should accept Jelmer's response below as it is complete and elegant. Just remember the concept of using this: it represents the calling object. Commented Dec 20, 2017 at 16:05

2 Answers 2

2

The secret is to use this to refer to a property of the own object

var f15c = { ... tailnumber: null, setTailNumber : function(tn) { this.tailnumber=tn; } }

Then:

f15c.setTailNumber(1234); console.log(f15c.tailnumber);

Sign up to request clarification or add additional context in comments.

Comments

2

Do you mean you want to set a value to a property?

var f15c = {
  _tailnumber: 0,
  set tailnumber(newtailnumber) {
    this._tailnumber = newtailnumber;
  },

  get tailnumber() {
    return this._tailnumber
  }
};

f15c.tailnumber = "304";
console.log(f15c.tailnumber);
console.log(f15c);

11 Comments

Still the full solution should contain a class: f15c isa plain (i guess), and an instance of f15c has a certain tail number.
Correct. If the object is reused one might think only the new set is applied, while all references are affected. But then we dive deep :-)
what does this helps you get tailnumber() { return this._tailnumber } rather than calling the f15c.tailnumber? also, i run the code snippet and not sure why did it return in the console.log(f15c); this reply { "_tailnumber": "304", "tailnumber": "304" } ? in my case i recieve console.log(f15c); console.log(f15c.tailnumber); console: Object {fuel: 10000, IRST: true, AESA: true, speed: 2500, Ammo: Object, …} 123
what does this helps you get tailnumber() { return this._tailnumber } rather than calling the f15c.tailnumber? ---- by using set and get on the same property name, you have a real set/get behavior: f15c.tailnumber(1) sets the number and f15c.tailnumber gets it for you. Then you have to store the actual value for tailnumber somewhere else. Using _tailnumber is nice because it keeps the name for simplicity.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.