Last active
January 2, 2016 19:39
-
-
Save yangjunjun/8352179 to your computer and use it in GitHub Desktop.
JS定义一个拥有私用属性的类,就是这个属性“只能”这个类的实例的方法能操作。
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/************************************************************** | |
* Dog相当一个命名空间,不能够产生实例 | |
**************************************************************/ | |
var Dog = { | |
name: '', | |
getName: function(){ | |
return this.name; | |
}, | |
setName: function(name){ | |
this.name = name; | |
} | |
} | |
var d1 = Dog; | |
var d2 = Dog; | |
d1 === d1; // output true | |
/************************************************************** | |
* Dog定义了一个类,可以在初始化的时候加入参数,产生不同的实例 | |
**************************************************************/ | |
var Dog = function(name){ | |
this.name = name; | |
} | |
Dog.prototype.getName = function(){ | |
return this.name; | |
} | |
Dog.prototype.setName = function(name){ | |
this.name = name; | |
} | |
var d1 = new Dog('X'); | |
var d2 = new Dog('Y'); | |
d1 === d2 ; // output false; | |
/************************************************************** | |
* Dog定义了一个类,利用闭包,定义了一个私有属性name | |
**************************************************************/ | |
var Dog = function(){ | |
var name = ''; | |
return { | |
getName:function(){ | |
return name; | |
}, | |
setName:function(v){ | |
name = v; | |
} | |
} | |
} | |
var d1 = Dog(); | |
d1.setName('X'); | |
d1.getName(); // output x | |
d1.name // output undefined | |
var d2 = Dog(); | |
d2.setName('Y'); | |
d2.getName(); // output Y | |
d2.name // output undefined | |
/************************************************************** | |
* Dog定义了一个类,利用闭包,定义了一个私有属性name | |
**************************************************************/ | |
var Dog = function(name){ | |
var name = name; | |
return function(){ | |
var F = function(){} | |
F.prototype.getName = function(){ | |
return name; | |
} | |
F.prototype.setName = function(v){ | |
name = v; | |
} | |
return new F(); | |
} | |
} | |
var d = Dog()(); | |
d.setName('YY') | |
d.getName() // output 'YY' | |
console.log(d.name) // output undefined | |
/************************************************************** | |
* 对上一个实现进行了优化,可以在初始化时添加参数 | |
**************************************************************/ | |
var Dog = function(name){ | |
return (function(name){ | |
var name = name; | |
return function(){ | |
var F = function(){} | |
F.prototype.getName = function(){ | |
return name; | |
} | |
F.prototype.setName = function(v){ | |
name = v; | |
} | |
return new F(); | |
} | |
})(name)(name); | |
} | |
var d = Dog('XX'); | |
d.getName(); // output 'XX' | |
d.setName('YY') | |
d.getName() // output 'YY' | |
console.log(d.name) // output undefined |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment