JS OO 学习笔记 ——JS封装使用prototype添加方法

function Person() { this.id = null; this.showId = function() { alert("My id is " + id); } Person.prototype.getId = function() { return id; } this.setId = function(newId) { id = newId; } } var p = new Person(); p.setId(1000); alert(p.id); p.showId(); var p2 = new Person(); alert(p.getId == p2.getId); // false

 

 

使用prototype使属性和方法共享,提高js效率。

 

你可能感兴趣的:(javascript)