Object constructor & this explained


#1


When I hit "save & submit" It's pretty obvious that my code isn't right....what am I doing wrong?


Replace this line with your code. 
var me = new Object();
me.name = "Cosmin";
me.age = 23;
console.log("My name is " + this.name + " and I have " + this.age);


#2

Why do you have the this keyword in your console.log statement ? You have to use the object.propertyname to access the property of the object you are trying to use.
Make sense?


#3

It works, ty:
var me = new Object();
me.name = "Cosmin";
me.age = 23;

console.log("My name is " + me.name + " and I have " + me.age + " years");


#4

@scriptrockstar46491
In short
the way you use this.name
the this keyword will point to the window object.....
which also called the global scope.

=========================================================================

Thus if you define a variable like
var me = 1;
you can get access to the associated Value of this me variable by using
console.log( window.me );
console.log(this.me);
or just
console.log( me );

=========================================================================

Now if you define a variable with an associated object Value
like
var me = { name: "james" , age:10 };
you can get access to the associated Value of the name property-key
of the me variable by using
console.log( window.me.name );
console.log(this.me.name);
or just
console.log( me.name );

=========================================================================

google search
== the Book ==
javascript [your question] site:developer.mozilla.org
javascript this site:developer.mozilla.org
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/this
Have a read through
http://stackoverflow.com/questions/4354418/var-vs-this-vs-constructor-parameter-variables
and
http://javascriptweblog.wordpress.com/2010/08/30/understanding-javascripts-this/
== discussions / opinions ==
https://www.codecademy.com/forum_questions/55f03d4b93767651db0005b3
javascript this explained site:stackoverflow.com
http://stackoverflow.com/questions/3127429/how-does-the-this-keyword-work
== guidance ==
www.crockford.com
http://javascript.crockford.com/code.html
http://javascript.crockford.com/survey.html
https://developer.mozilla.org/en-US/docs/Web/JavaScript/A_re-introduction_to_JavaScript

============================================================

Have a read through
http://stackoverflow.com/questions/4354418/var-vs-this-vs-constructor-parameter-variables
and
http://javascriptweblog.wordpress.com/2010/08/30/understanding-javascripts-this/


#5

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.