1.封装性
/**
- 利用函数创建的作用域达到数据封装的目的。
- @type {{getName, setName}}
*/
var obj=(function () {
var _name="gcy";
return {
getName:function () {
return _name;
},
setName:function (val) {
_name=val;
}
};
})();
obj._name; //undefined
obj.getName(); //gcy
2.继承性
/**
* 简单的es5原型继承
* @constructor
*/
var A=function () {
}
A.prototype={name:"gcy"};
var B=function () {
};
B.prototype=new A();
var b=new B();
console.log(b.name);
/**
* e6继承实现demo
*/
class People{
constructor(name){
this.name=name;
}
getName(){
return www.90168.org
this.name;
}
}
class Black extends People{
constructor(name){
super(name);
}
speak(){
return " i am black";
}
}
var peo=new Black("gcy");
console.log(peo.getName()+' says '+peo.speak());
3.多态性
/**
* 多态的实现案例
* @param animal
*/
var makeSound=function (animal) {
animal.sound();
}
var Cat=function () {
}
var Dog=function () {
}
Cat.prototype.sound=function () {
console.log("喵喵喵")
}
Dog.prototype.sound=function () {
console.log("旺旺旺")
}
makeSound(new Cat());
makeSound(new Dog());