function Person(name) {
this.name = name;
this.say = function() {
console.info('I am ' + this.name);
}
}
var p=new Person('stackoverflow');
Someone will tell me that the codes above are equal:
function Person(name) {
this.name = name;
this.say = function() {
console.info('I am ' + this.name);
}
}
var p={};
Person.call(p,'stackoverflow');
It's true?
If so, what about the prototype?
Each object in javascripte owns a prototype, and the prototype chain keeps exempt from obejcts, I wonder if this prototype does something or not.
In this example, when the 'p' object is created, does it invoke some inline method of the Person superclass?
By the way, I want to know what the syntax does var p=new Person('stackoverflow');?
----------------- update ------------------
function Person(name) {
this.name = name;
}
Person.prototype.say = function() {
console.info('I am ' + this.name);
}
How about if I put protection in human function:
function Person(name) {
this.name = name;
Person.prototype.say = function() {
console.info('I am ' + this.name);
}
}
source
share