0

What is the difference between this...

function Person(name, age, sex) {
  this.name = name;
  this.age = age;
  this.sex = sex;
}

and this...

function Person(name) {
  this.name = name;
  this.age = age;
  this.sex = sex;
}

and this...

function Person() {
  this.name = name;
  this.age = age;
  this.sex = sex;
}

Thanks!


David, I have a piece of code for a game I am building that looks like this...

function Player(node){
    this.node = node;
    this.grace = false;
    this.replay = 3; 
    this.shield = 3;
    this.respawnTime = -1;
  ...
return true;
}

and it does not return a reference error. Its an object for a player in a javascript game I am building...

0

3 Answers 3

2

As Brett said, the second and third constructor function will assign any global variable to the properties of the new Person instance. However, you can still use any argument that might be passed to the constructor:

function Person()//no args
{
    //depending on which value is available:
    //prop = n-th argument OR global variable OR undefined
    this.name = arguments[0] || window.name || undefined;
    this.age = arguments[1] || window.age || undefined;
    this.sex = arguments[2] || window.sex || undefined;
}
var parrot = new Person('polly',1,'M');
console.log(parrot.name);//polly
var noSex = new Person('Joe',99);//if no global sex is set:
console.log(noSex.sex);//undefined
var sex = 'F';
var jane = new Person('Jane',25);
console.log(jane.sex);//F
Sign up to request clarification or add additional context in comments.

6 Comments

So if I was to intentionaly avoid global variables or undefined variables, then its a good practice to to always set my local variables as parameters for my constructor functions?
Yes, or declare them inside the function body. regardless of that function being a constructor or not. If you don't expect that variable to be passed as an argument, you can declare it inside the scope of the constructor and, for example, use it as a reference to the object. Like you might have seen: var that = this; in a constructor is such a variable, but equally valid would have been function SomeConstructor(arg1,arg2,that){ that = this;
@blachawk: You're welcome... just a side-note on your update: though it probably won't cause any issues, it's not the best of ideas to return true from a constructor. Just omit the return statement, because constructors implicitly return an object, which (with the possible exception of the boolean constructor) will never be a falsy value. Also, if you don't want to create accidental globals, add 'use strict'; as a first line to your constructors, that way Errors will be thrown if you forget the new keyword. In non-strict mode this points to the global object, setting this.name etc...
thanks for the tip. can i return true from within a conditional statement that is inside of my construtor? or is that still not a good idea?
@blackhawk: A constructor's job is to return an object. if you insist on explicitly returning something, you could write return this;, because that's what JS does anyway. returning something else will just cause confusion with people who end up using the code, or even yourself. I'd say: no, never use return true in a constructor. The only time you might do so is when you check if the new keyword was used: function MyConstr(){ if (this === window){return false;}, returning false if this points to the global object
|
2

The first relies on local variables set through parameters to set its instance variables/properties, the third relies on globals to set its properties, and the second is a mix.

Comments

0

1st one is valid, you initite your local variables with the parameters sent in the constructor. the rest doesnt really make sense.

in the 2nd one you kind of mixing parameters with some other variables which you can access at that point, you initate only the name using a parameter.

and the 3rd one is based on accessable variables, but none of them is given as a parameter, i never saw something usefull using 2nd and 3rd options..

2 Comments

Why is it so important to initiate local variabls with the parameters? what is it that you can do with the 1st one, that becomes cloudy or of no use with the 2nd and 3rd? thanks!
I didnt say its important to initiate local variabls with the parameters? just iniating local variables in a constructer doesnt makes sense if you dont use any statewhich you set using params, because then you can iniate them directly in the declaration.

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.