Because the f1 is the function. And expression 'this.v1 = 5' is in it's body. So you must execute function at least.
Also, 'this' is passed to the function by callee. If you just call the function, you will not pass 'this' value, so it will refer to the global object. If you use the 'new' keyword, this will point to the just created object. Also, the 'prototype' is just a property. It only used to create a new object, or to determine 'class' (constructor function) of the object. So value you assigned to it is not used until you use the 'new' keyword. 'new' keyword creates an empty object, populates it with properties/methods contained in 'prototype' property and calls the constructor function, passing just created object as 'this' reference, returns this object after that as result. Regards, Max. On Mon, Apr 11, 2011 at 5:15 PM, <[email protected]> wrote: > Hello, > > I start to learn JS. > > ************************** > var f1 = function () { > this.v1 = 5; > } > f1.prototype.get_v1 = function () { return this.v1; }; > var f2 = new f1(); > > f1.v1 // return nothing > f1.get_v1() // nothing > f2.v1 // return 5 > f2.get_v1() // return 5 > > > ************************** > > Why f1.v1 and f1.get_v1 return nothing ? > > > > > I read documentations, try to understand, but I think that I need more > explanation. > Thank you. > Sam > > -- > To view archived discussions from the original JSMentors Mailman list: > http://www.mail-archive.com/[email protected]/ > > To search via a non-Google archive, visit here: > http://www.mail-archive.com/[email protected]/ > > To unsubscribe from this group, send email to > [email protected] > -- Максим Васильев Моб.: +7 915 192-27-24 -- To view archived discussions from the original JSMentors Mailman list: http://www.mail-archive.com/[email protected]/ To search via a non-Google archive, visit here: http://www.mail-archive.com/[email protected]/ To unsubscribe from this group, send email to [email protected]
