Prototype


prototype属性允许您向任何对象添加属性和方法(Number,Boolean,String,Date等)。

注 - Prototype是一个全局属性,几乎可用于所有对象。

语法

string.prototype

示例:对象原型

function employee(id, name) {
   this.id = id;
   this.name = name;
}
var emp = new employee(123, "Smith");
employee.prototype.email = "smith@abc.com";

console.log("Employee 's Id: " + emp.id);
console.log("Employee's name: " + emp.name);
console.log("Employee's Email ID: " + emp.email);

输出

Employee’s Id: 123 
Employee’s name: Smith
Employee’s Email ID: smith@abc.com