0

i was reading in a book and here the code is

function Person(name) {

    this.name = name;
}

Person.prototype.legs = 2;

var newperson = new Person("Joe"), prop;


for (prop in newperson) {
    if (newperson.hasOwnProperty(prop)) {
        console.log(prop + ":" + newperson[prop]);

    }

}

I just want to know what is **prop* in this

var newperson = new Person("Joe"), prop;

It is not a argument for sure so what does prop mean here. is it an optional value? thanks

2
  • 2
    , prop; is just the definition of the variable prop Commented Sep 29, 2015 at 10:03
  • 1
    Nicely obfuscated to induce headaches in newbies. Never write code like this, it just causes questions like this. Commented Sep 29, 2015 at 10:06

2 Answers 2

2
var newperson = new Person("Joe"), prop;

is equivalent to

var newperson = new Person("Joe");
var prop;
Sign up to request clarification or add additional context in comments.

Comments

-1

JavaScript follows lexical scoping of variables.

During the compilation phase, JavaScript engine will only catch up the variable declarations.

So, var newperson = new Person("Joe"), prop; after the compilation phase both variables are stored in the scope with no difference. Both are undefined.

At the execution phase, JavaScript will find the initialization of newperson as a Person object. prop is not initialized yet thus undefined.

Comments

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.