3

we can see

function generate() {

this.a = 'hello';

}

obj = new generate();

so, my question is, how to understand 'this.' phenomenon?

Determinant
  • 3,886
  • 7
  • 31
  • 47
  • possible duplicate of [What is 'this' before an object is instantiated in js?](http://stackoverflow.com/questions/1283636/what-is-this-before-an-object-is-instantiated-in-js) – Thilo Dec 27 '11 at 08:01

3 Answers3

4

Yes, functions are also objects in Javascript. Read more about it at http://en.wikipedia.org/wiki/First-class_function

WTK
  • 16,583
  • 6
  • 35
  • 45
2

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

nnnnnn
  • 147,572
  • 30
  • 200
  • 241
  • can you give more specific explanation about the process of new generate() ? – Determinant Dec 27 '11 at 08:32
  • Did you read the page I linked to? Or here's another article about it (with diagrams): http://mckoss.com/jscript/object.htm – nnnnnn Dec 27 '11 at 08:41
  • Sorry, I couldn't open it just now. thank you~ I think I've got the answer. BTW, do you have some good materials about learning Javascript? – Determinant Dec 27 '11 at 08:47
1

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();)

Flavien Volken
  • 19,196
  • 12
  • 100
  • 133
  • Functions _are_ objects. Try `function test() {}` and then `test.prop = "1";` – nnnnnn Dec 27 '11 at 08:28
  • ok it's a first class object, but they are not pure objects in themselves. What I thought which was confusing in the question was the usage of the "this" operator after a "new" one. Which in this case the function operates on the new created object and not on the function one itself. – Flavien Volken Dec 27 '11 at 08:32
  • What's a "pure object"? I don't understand the distinction you are trying to make. – nnnnnn Dec 27 '11 at 08:35
  • `typeof({})` returns `"object"` for example. But `{}` cannot be executed. – Flavien Volken Dec 27 '11 at 08:37
  • can generate() be called directly although there's 'this' inside? what's the effect? – Determinant Dec 27 '11 at 08:37
  • you can call "generate" from outside but in this case this will be the method itself and then the declared "a" is useless. It would have been here: `function generate() { this.a = 'hello'; alert(this.a); }` – Flavien Volken Dec 27 '11 at 08:41
  • I completed my answer to help you understand how to declare public vs private items within a class. Something very important while coding in js. – Flavien Volken Dec 27 '11 at 09:17