All JS objects inherit their properties/methods from their prototype.
The standard way to create an object prototype is to use object constructor function:
function person(first, last age) { this.firstName = first; this.lastName = last; this.age = age; }; var farther = new person("Frank", "Zhang", 40); farther.Country = "Canada"; //Add methods/property to the exist object father.name = function () { return this.firstName + " " +this.lastName; }You can extend the object with new function and new properties as above. However, the new added function/property are limited only for the class you defined as above.
If you want to add function/property for all person object, you need to user prototype.
person.prototype.name = function () { return this.firstName + " " + this.lastName; };
Last but not least, Never modify the prototypes of Standard Javascript Objects.
No comments:
Post a Comment