Yes var fn = function(){};
is of type Funcion
Why is test.porototype empty: Beacuse it's Function.prototype, not Object.prototype
What is this: answered in the link posted in the end
You can use a function as a constructor to create objects:
var Person = function(name){
this.name = name;
};
Person.prototype.walk=function(){
this.step().step().step();
};
In the example the Person is called a constructor function as it's an object you can give it properties as well like: Person.static="somthing"
this is good for static members related to Person like:
Person.HOMETOWN=22;
var ben = new Person("Ben");
ben.set(Person.HOMETOWN,"NY");
ben.get(Person.HOMETOWN);//generic get function what do you thing it'll get for ben?
ben.get(22);//maybe gets the same thing but difficult to guess
When you crate an instance using Person you have to use the new keyword:
var bob = new Person("Bob");console.log(bob.name);//=Bob
var ben = new Person("Ben");console.log(bob.name);//=Ben
The property/member name
is instance specific, it's different for bob and ben
The member walk can be used on all instances (bob and ben are instances of Person)
bob.walk();ben.walk();
Because walk() could not be found on bob (and later ben) JavaScript will look for it in the Person.prototype. There is only one walk function both bob and ben share this but the function will behave differently because in the walk function it uses this
.
If ben was waiting for a red light and you'll invoke walk and bob was at a green light then obviously something different would happen to ben and bob even though walk
does exactly the same for bob and ben but this
will refer to current object (bob for bob and ben for ben).
Shadowing members happens when I do something like ben.walk=22
, even though bob and ben share walk
the assignment of 22 to ben.walk will not effect bob.walk. This is because that statement will create a member called walk
on ben and assign it a value of 22. When asking for bob.walk you'll get the Person.prototype.walk function because walk
could not be found on bob. Asking for ben.walk however will get you the value 22 because the member walk has been created on ben and since JavaScript found walk on ben it will not look in the Person.prototype.
So when a member cannot be found on the instance the JavaScript engine will look on the constructor's prototype. So where does ben.hasOwnProperty come form? The instance ben doesn't have it, Person.prototype doesn't have it. The answer is that there is a prototype chain, Person's constructor is Function so when hasOwnProperty cannot be found on ben or or Person.prototype then it'll look on Person's constructor's prototype (is Funcition). If it cannot find hasOwnProperty there (but it can) then it'll look in Function's constructor's prototype (is Object).
Everything should inherit from Object.prototype unless you do Object.create(null,{})
which in my opinion is a bad idea. You can add things on Object.prototype (not a good idea either) and any object in JS will "inherit" that:
Object.prototype.yipee=function(){
console.log("yipeee");
};
22..yipee();//logs yipeee
"hello".yipee();//logs yipee
Now that you got the basics of it you can check out the following:
https://stackoverflow.com/a/16063711/1641941 (start with "the this variable" at the end)