we can see
function generate() {
this.a = 'hello';
}
obj = new generate();
so, my question is, how to understand 'this.' phenomenon?
we can see
function generate() {
this.a = 'hello';
}
obj = new generate();
so, my question is, how to understand 'this.' phenomenon?
Yes, functions are also objects in Javascript. Read more about it at http://en.wikipedia.org/wiki/First-class_function
Yes, functions are objects.
But there is more to it than that in your sample code. If you call a function with the new
keyword JavaScript creates a new object for you and then uses the function as the constructor: within the function this
references the newly created object, and the new object will be returned - but this new object is not the function itself: to oversimplify, if your function is generate()
then the new object is based on generate.prototype
, which by default is an empty object.
Further reading: https://developer.mozilla.org/en/Introduction_to_Object-Oriented_JavaScript
JS being totally dynamic, functions are not pure objects but can be created, read, written, deleted and called on the fly in the same manner as objects.
proof: typeof(function(){})
returns "function"
The usage of the "this"
in a function is ofter used to describe a pseudo constructor:
As there is no classes in javascript (js is completely declarative), a way to do something similar is to actually write functions which will affect the self (this) instance. In your case the call of your "generate" function through the "new" operator will actually create an empty object and then throw your "generate" function as if it had been into the object itself. You then have a method to build an object containing a public value "a" containing "hello".
[edit] Ok to clarify how those pseudo constructors works: At first, why not a constructor ? Simply because we are describing the current object and not creating one from a class. (remember there is no classes in javascript)
function pseudoClass()
{
this.a = "hello";// This is a public variable. See below for example
var b = "hi"; // This is a private variable i.e. accessible only within the scope of those brackets
c = "ciao"; // This is a global variable, usually pretty bad to put that here... but why not
alert(this.a); // Alerts "hello" as this is the current object;
alert(a); // Error, "a" is not a local variable, only the current object one (would be undefined)
alert(b); // Alerts "hi", b is accessible as long as we are within the same brackets (scope) as where it does have been described.
alert(c); // Alerts "ciao", "c" is now available everywhere…
}
Then from outside
var o = new pseudoClass();
alert(o.a); // Alerts "hello", remember "a" is public and then accessible from the outside
alert(o.b); // Will not work, b is private…
alert(o.c); // Will not work, c is global and therefore not related to o.
alert(c); // Alerts "ciao", c being global, once defined it is available everywhere.
Explicitely "o" looks like:
{"a":"hello"}
Note that you could have affected functions to "a" (public) and "b" (private) the same way you affected them "Strings" i.e. this.a = function(){ /* do Something */}
and var b = function(){ /* do Something else */}
. (the call from outside is then now o.a();
)