this is #33 from John Resig`s Learning Advanced JavaScript. http://ejohn.org/apps/learn/#33 Would appreciate as much help as you can provide on this.
1) technically speaking, is ninja.changeName("Bob")
"calling" the function Ninja, or does it go immediately to this.changeName(name);
2) Once ninja.changeName("Bob")
is invoked, what is the order in which the processing events take place inside function Ninja(name)?
3) what exactly is the purpose/function of this.changeName ( name);
function Ninja(name){
this.changeName = function(name){
this.name = name;
};
this.changeName( name );
}
var ninja = new Ninja("John");
assert( ninja.name == "John", "The name 开发者_如何学编程has been set on initialization" );
ninja.changeName("Bob");
assert( ninja.name == "Bob", "The name was successfully changed." );
1) technically speaking, is ninja.changeName("Bob") "calling" the function Ninja, or does it go immediately to this.changeName(name);
The function Ninja
can be only called with the syntax Ninja(..)
. The syntax new Ninja(...)
doesn't exactly call the function, it uses it as a constructor for a new object. And the syntax ninja.changeName("Bob")
calls the anonymous function attached to the changeName
property of the ninja
object by its Ninja
constructor. ninja.changeName("Bob")
has very few things to do with the Ninja
function, "technically speaking". Once the object ninja
has been created, there is no relation at all between ninja.changeName
and Ninja
.
Returning to your question: neither. It doesn't "call" the function Ninja
, and it doesn't go to this.changeName(name)
. It simply calls the function ninja.changeName
, which is a regular, anonymous function, referenced by a property of our object ninja
.
2) Once ninja.changeName("Bob") is invoked, what is the order in which the processing events take place inside function Ninja(name)?
As written above, there's no relation between the two. Executing ninja.changeName("Bob")
does a call
on the anonymous function referenced by ninja.changeName
. The association between ninja.changeName
and the anonymous function function(name) { this.name=name; }
has been set by the constructor Ninja
, but that's the only relation between them.
3) what exactly is the purpose/function of this.changeName ( name);
The function changeName
is called in the constructor to show you that a method can be called inside the constructor. You can create setters for an object, then use these setters in the constructor to initialize the object's properties. The example is didactic ; in the real world, setters can be quite complicated, and you have to use them to initialize the object. And besides, what would be the point of creating a setter if don't use it, like in :
function Ninja(constructorArg) {
this.changeName = function(setterArg){
this.name = setterArg;
};
this.name = constructorArg; // I'm not using the setter I've just written
}
1) It is calling the method on the function.
2) It simply updates its own name
property via the setter.
3) It is to use a setter to change a property.
1) technically speaking, is ninja.changeName("Bob") "calling" the function Ninja, or does it go immediately to this.changeName(name);
It calls the anonymous function that is assigned to the changeName
property of the instance of Ninja that is created with var ninja = new Ninja("John");
.
2) Once ninja.changeName("Bob") is invoked, what is the order in which the processing events take place inside function Ninja(name)?
The single statement in that function is run
3) what exactly is the purpose/function of this.changeName ( name);
It takes one argument, and assigns the value of that argument to the name
property of the instance of Ninja
on which the method is called.
this is very good question)
in JS functions are objects, so by calling
ninja.changeName("Bob")
you go straight to ninja's method.changeName()
if
ninja.changeName
was called no actions outside this method will be firedthis.changeName ( name);
- is constructor actions, they are applied to ninja object on creation only (to extend the newly created object with property name)
take a look at this great book on OOP in JS
精彩评论