//ex1 工厂模式
function creatPerson(name,age,job){
var o = new Object();
o.name = name;
o.age = age;
o.job = job;
o.sayName = function(){
console.info(name);
}
return o;
}
var person1 = creatPerson("je",18,"stduent");
var person2 = creatPerson("ji",28,"man");
console.info(person1 instanceof creatPerson);
console.info(person2);
//ex2 构造函数
function Person(name,age,job){
this.name = name;
this.age = age;
this.job = job;
this.sayName = function(){
console.info(name);
}
}
var person1 = new Person("je",18,"stduent");
var person2 = new Person("ji",28,"man");
console.info(person1 instanceof Person); //判断哪个类
console.info(person1.constructor==Person); //指向类 ,一般用instanceof
console.info(person2);
//ex2.1构造函数优化--没有封装性
function Person(name,age,job){
this.name = name;
this.age = age;
this.job = job;
this.sayName = sayName;
}
function sayName(){
console.info(name);
}
var person1 = new Person("je",18,"stduent");
//ex3 原型模式
function Person(){
}
Person.prototype.name = 'Je';
Person.prototype.age = '29';
Person.prototype.job = 'student';
Person.prototype.sayName = function(){
console.info(this.name);
}
var person1 = new Person();
var person2 = new Person();
console.info(person1.sayName==person2.sayName); //引用的是同一个方法
console.info(Person.prototype); //指向原型对象
console.info(Person.prototype.constructor); //原生的Person 函数
console.info(person1.prototype); //undefined
console.info(person1.prototype.constructor); // 'constructor' of undefined
console.info(Person.prototype.isPrototypeOf(person1)); //true 用来确定实例与原型是否有关系
console.info(Object.getPrototypeOf(person1)); //Sc5的新方法 ==Person.prototype ie9支持
//ex3.1 delete、 hasOwnProperty使用
//获取所有实例方法 Object.keys();--可枚举
//Object.getOwnPropertyNames();--不可枚举
function Person(){
}
Person.prototype.name = 'Je';
Person.prototype.age = '29';
Person.prototype.job = 'student';
Person.prototype.sayName = function(){
console.info(this.name);
}
console.info(Object.keys(Person.prototype)); //["name", "age", "job", "sayName"]
var person1 = new Person();
person1.name = 'ne';
console.info(Object.keys(person1)); //["name"]
console.info(Object.getOwnPropertyNames(person1)); //["name"]
console.info(person1.name); //ne
console.info(person1.hasOwnProperty("name")); //true 判断属性是在原型中还是实例中,实例中为true
delete person1.name ; //delete只会删除实例的属性/方法 不会删除原型
console.info(person1.hasOwnProperty("name")); //false
console.info(person1.name); //Je
//ex3.2 更简单的原型方法
function Person(){
}
Person.prototype = {
constructor : Person, //默认下不再指向Person,有必要可以在这添加,但[Enumerable]属性变为true,默认为false;
name : 'je',
age : '29',
job : 'student',
sayName : function(){
console.info(this.name);
}
}
console.info(Object.keys(Person.prototype)); //["constructor", "name", "age", "job", "sayName"]
//ex3.3 constructor的Enumerable属性
function Person(){
}
Person.prototype = {
name : 'je',
age : '29',
job : 'student',
sayName : function(){
console.info(this.name);
}
}
//重设构造函数,只适用ECMA5兼容的浏览器
Object.defineProperty(Person.prototype,"constructor",{
enumerable : false,
value : Person
});
console.info(Object.keys(Person.prototype)); //["name", "age", "job", "sayName"]
//ex4 组合使用构造函数模式和原型模式
function Person(name,age,job){
this.name = name;
this.age = age;
this.job = job;
this.friends = ["je","no"];
}
Person.prototype = {
constructor : Person,
sayName : function(){
console.info(this.name);
}
}
var person1 = new Person("je",18,"stu");
var person2 = new Person("ni",20,"st");
person1.friends.push("ke");
console.info(person1.friends); //["je", "no", "ke"]
console.info(person2.friends);
//["je", "no"]
console.info(person1.sayName==person2.sayName); //true
//ex 5 动态原型模式
function Person(name,age,job){
this.name = name;
this.age = age;
this.job = job;
//方法
if(typeof this.sayName != "function"){
Person.prototype.sayName = function(){
console.info(this.name);
}
}
}
//ex 6 寄生构造函数--不能使用instanceof 确定对象类型
function Person(name,age,job){
var o = new Object();
o.name = name;
o.age = age;
o.job = job;
o.sayName = function(){
console.info(this.name);
}
return o;
}
var friend = new Person("je",18,"stu");
friend.sayName(); //je
//ex 6.1
function SpecialArray(){
var values = new Array();
values.push.apply(values,arguments);
values.toPipedString = function(){
return this.join("|");
}
return values;
}
var color = new SpecialArray("red","blue","green");
console.info(color.toPipedString()); //red|blue|green
//ex 7 稳妥构造函数--不用this和new ,instanceof没用
function Person(name,age,job){
var o = new Object();
o.name = name;
o.age = age;
o.job = job;
o.sayName = function(){
console.info(name);
}
return o;
}
var friend = Person("je",18,"stu");
friend.sayName(); //je
//ex 8 原型链
function Supertype(){
this.property = true;
}
Supertype.prototype.getSuperValue = function(){
return this.property;
};
function Subtype(){
this.subproperty = false;
}
//继承Supertype
Subtype.prototype = new Supertype();
Subtype.prototype.getSubvalue = function(){
return this.subproperty;
}
var instance = new Subtype();
console.info(instance.getSuperValue()); //true
//ex 8.1
function Supertype(){
this.property = true;
}
Supertype.prototype.getSuperValue = function(){
return this.property;
};
function Subtype(){
this.subproperty = false;
}
//继承Supertype
Subtype.prototype = new Supertype();
//添加新方法
Subtype.prototype.getSubvalue = function(){
return this.subproperty;
}
//重写超类中的方法
Subtype.prototype.getSuperValue=function(){
return false;
}
var test = new Supertype();
var instance = new Subtype();
console.info(instance.getSuperValue()); //false
console.info(test.getSuperValue()); //true
//ex 9借用构造函数
function Supertype(){
this.color = ["red","blue","green"];
}
function SubType(){
//继承Supertype
Supertype.call(this);
}
var instance1 = new SubType();
instance1.color.push("black");
console.info(instance1.color); //["red", "blue", "green", "black"]
var instance2 = new SubType();
console.info(instance2.color); //["red", "blue", "green"]