S
Stone Zhong
Hi there,
I am reading the book "The Concise Guide to Dojo" and I saw some code
like below on page 11:
decafbad.school.PersonClassic.prototype = {...
I personally did a test like below
==== this code does not work ===
function Person(name) {
this.name = name;
Person.prototype = {
sayHi: function() { alert('hi ' + this.name); }
};
}
var a = new Person("JavaScript");
a.sayHi(); // I got error a.sayHi is not a function
however, if I change the code to below style , it worked,
function Person(name) {
this.name = name;
Person.prototype.sayHi = function() { alert('hi ' + this.name);
}
var a = new Person("JavaScript");
a.sayHi();
My conclusion is, a function's prototype already has some hidden
properties which might be important, replace function's prototype is
not good idea, only add property to function's prototype.
I am reading the book "The Concise Guide to Dojo" and I saw some code
like below on page 11:
decafbad.school.PersonClassic.prototype = {...
I personally did a test like below
==== this code does not work ===
function Person(name) {
this.name = name;
Person.prototype = {
sayHi: function() { alert('hi ' + this.name); }
};
}
var a = new Person("JavaScript");
a.sayHi(); // I got error a.sayHi is not a function
however, if I change the code to below style , it worked,
function Person(name) {
this.name = name;
Person.prototype.sayHi = function() { alert('hi ' + this.name);
}
var a = new Person("JavaScript");
a.sayHi();
My conclusion is, a function's prototype already has some hidden
properties which might be important, replace function's prototype is
not good idea, only add property to function's prototype.