3

we can see

function generate() {

this.a = 'hello';

}

obj = new generate();

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

1

3 Answers 3

4

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

Sign up to request clarification or add additional context in comments.

Comments

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

3 Comments

can you give more specific explanation about the process of new generate() ?
Did you read the page I linked to? Or here's another article about it (with diagrams): mckoss.com/jscript/object.htm
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?
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();)

7 Comments

Functions are objects. Try function test() {} and then test.prop = "1";
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.
What's a "pure object"? I don't understand the distinction you are trying to make.
typeof({}) returns "object" for example. But {} cannot be executed.
can generate() be called directly although there's 'this' inside? what's the effect?
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.