JavaScript面向对象编程
suoyuesmile opened this issue · 3 comments
suoyuesmile commented
suoyuesmile commented
实例化对象的3种方法
// 1.生成实例对象的原始模式
var Cat = {
name : '',
color : ''
}
var cat1 = {};
cat1.name = "大毛";
cat1.color = "黄色";
var cat2 = {};
cat2.name = "二毛";
cat2.colot = "黑色";
// 改进
function Cat(name, color) {
return {
name:name,
color:color
}
}
var cat1 = Cat("大毛","黄色");
var cat2 = Cat("二毛","黑色");
// 2.构造函数
function Cat(name, color) {
this.name = name;
this.color = color;
}
var cat1 = new Cat("大毛","黄色");
var cat2 = new Cat("二毛","黄色");
alert(cat1.name);
alert(cat1.color);
// 3.Prototype模式
function Cat(name, color) {
this.name = name;
this.color = color;
}
Cat.prototype.type = "猫科动物";
Cat.prototype.eat = function(){
alert("吃老鼠");
};
var cat1 = new Cat("大毛","黄色");
var cat2 = new Cat("二毛","黑色");
alert(cat1.type);
cat1.eat();
alert(Cat.prototype.isPrototypeOf(cat1));
alert(Cat.prototype.isPrototypeOf(cat2));
alert(cat1.hasOwnProperty("name"));
alert(cat1.hasOwnProperty("type"));
alert("name" in cat1);
alert("type" in cat1);
for(var prop in cat1) {
alert("cat1[" + prop + "]=" + cat1[prop]);
}
```
suoyuesmile commented
构造函数实现继承的5种方法
function Animal(){
this.species = "动物";
}
function Cat(name, color){
this.name = name;
this.color = color;
}
//1. 构造函数绑定
function Cat(name, color) {
Animal.apply(this, arguments);
this.name = name;
this.color = color;
}
ar cat1 = new Cat("大毛","黄色");
alert(cat1.species);
//2. prototype模式
Cat.prototype = new Animal();
//删除Cat的prototype对象,赋予一个新值
Cat.prototype.constructor = Cat;
//手动纠正继承链
vat cat1 = new Cat("大毛","黄色");
alert(cat1.species);
//3. 直接继承prototype
function Animal(){ }
Animal.prorotype.species = "动物";
Cat.prototype = Animal.prototype;
Cat.prototype.constructor = Cat;
var cat1 = new Cat("大毛","黄色");
alert(cat1.species);
//实际把Animal的原型构造也改变了
alert(Animal.prototype.constructor);
//4. 利用空对象为中介
var F = function(){};
F.prototype = Animal.prototype;
Cat.prototype = new F();
cat.prototype.constructor = Cat;
alert(Animal.prototype.constructor);
//封装成函数
function extend(Child, Parent) {
var F = function(){};
F.prototype = Parent.prototype;
Child.prototype = new F();
Child.prototype.constructor = Child;
Child.uber = Parent.prototype;
//实现继承的完备性
}
extend(Cat, Animal);
var cat1 = new Cat("大毛","黄色");
alert(cat1.species);
//5. 拷贝继承
function Animal(){}
Animal.prototype.speices = "动物";
function extend2(Child,Parent){
var p = Parent.prototype;
var c = Child.prototype;
for (var i in p) {
c[i] = p[i];
}
c.uber = p;
}
//将父亲对象的原先属性全部拷贝给子
suoyuesmile commented
非构造函数实现继承的3种方法
var Chinese = {
nation: "**"
};
var Doctor = {
career: "医生";
};
//1. Object方法
function object(o) {
function F() {}
F.prototype = o;
return new F();
}
var Doctor = object(Chinese);
//2. 浅拷贝
function extendCopy(p) {
var c = {}
for (var i in p){
c[i] = p[i];
}
c.uber = p;
return c;
}
var Doctor = extendCopy(Chinese);
//3.深拷贝
function deepCopy(p, c){
var c = c || {};
for (var i in p){
if(typeof p[i] === 'object'){
c[i] = (p[i].constructot === Array) ? [] : {};
deepCopy(p[i], c[i]);
}else {
c[i] = p[i];
}
return c;
}
}