javascript object oriented programming (一)
2011-06-19 12:19 jalen 阅读(349) 评论(0) 收藏 举报/*
* 1. 生成对象的原始模式
* 这就是最简单的封装了。但是,这样的写法有两个缺点
* 1.一是如果多生成几个实例,写起来就非常麻烦
* 2.二是实例与原型对象之间,没有任何办法,可以看出有什么联系
*/
var Cat = {
name : '',
color : ''
}
var cat1 = {}; // 创建一个空对象
cat1.name = "大毛"; // 按照原型对象的属性赋值
cat1.color = "黄色";
var cat2 = {};
cat2.name = "二毛";
cat2.color = "黑色";
/*
* 2. 原始模式的改进,我们可以写一个函数,解决代码重复的问题。
* 这种方法的问题依然是,cat1和cat2之间没有内在的联系,不能反映出它们是同一个原型对象的实例。
*/
function Cat(name,color){
return {
name:name,
color:color
}
}
var cat1 = Cat('cat1','red');
var cat2 = Cat('cat2','blue');
console.log(cat1);
console.log(cat2);
/*
* 3. 构造函数模式
* 所谓"构造函数",其实就是一个普通函数,但是内部使用了this变量。
* 对构造函数使用new运算符,就能生成实例,并且this变量会绑定在实例对象上
*
*/
function Cat(name,color){
this.name = name;
this.color = color;
}
//生成实例对象
var cat1 = new Cat('大黄','yellow');
var cat2 = new Cat('大黑','black');
console.log(cat1);
console.log(cat2);
//cat1和cat2会自动含有一个constructor属性,指向它们的构造函数
console.log(cat1.constructor === Cat); //true
console.log(cat2.constructor === Cat); //true
//Javascript还提供了一个instanceof运算符,验证原型对象与实例对象之间的关系。
console.log(cat1 instanceof Cat) //true
console.log(cat2 instanceof Cat) //truev
/*
* 4. 构造函数模式的问题
* 每一个实例对象,type属性和eat()方法都是一模一样的内容,
* 每一次生成一个实例,都必须为重复的内容,多占用一些内存。这样既不环保,也缺乏效率。
* 所以有了prototype,可以把共享的方法放到prototype里。每个实例对象特有的方法放在构造函数里
*/
function Cat(name,color){
this.name = name;
this.color = color;
this.type = "猫科动物";
this.eat = function(){alert("吃老鼠");};
}
var cat1 = new Cat("大毛","黄色");
var cat2 = new Cat ("二毛","黑色");
console.log(cat1.type); // 猫科动物
cat1.eat(); // 吃老鼠
//可以让type属性和eat()方法在内存中只生成一次,然后所有实例都指向那个内存地址呢?回答是可以的。
console.log(cat1.eat == cat2.eat); //false
/*
* 5. Prototype模式
* Javascript规定,每一个构造函数都有一个prototype属性,指向另一个对象(其实就是prototype对象,)。
* 这个对象的所有属性和方法,都会被构造函数的实例继承。
* 我们只需要把想让实例对象共享的对象和方法放到prototype对象里就可以了。
*/
function Cat(name,color){
this.name = name;
this.color = color;
}
Cat.prototype.type = '猫科动物'; //可以理解为属性
Cat.prototype.eat = function(){alert('吃老鼠')}; //可以理解为方法
var cat1 = new Cat('大黄','yellow');
var cat2 = new Cat('大蓝','blue');
console.log(cat1.type);
cat1.eat();
//所有实例的type属性和eat()方法,其实都是同一个内存地址,指向prototype对象,因此就提高了运行效率。
console.log(cat1.eat === cat2.eat); //true cat1和cat2的eat方法的引用是同一个内存地址。
/* 6. Prototype模式的验证方法 */
//6.1 isPrototypeOf()这个方法用来判断,某个proptotype对象和某个实例之间的关系。
console.log(Cat.prototype.isPrototypeOf(cat1)); //实例对象cat1是否与prototype对象有关系,true
console.log(Cat.prototype.isPrototypeOf(cat2));
//6.2 hasOwnProperty()
//每个实例对象都有一个hasOwnProperty()方法,用来判断某一个属性到底是本地属性,还是继承自prototype对象的属性。
//如果是prototype对象上的属性 返回false.
console.log(cat1.hasOwnProperty('name')); //是本地属性,构造函数内的,不是继承prototype上的属性。返回true
console.log(cat1.hasOwnProperty('type')); //继承prototype对象的属性,返回false
console.log(cat1.hasOwnProperty('eat')); //同type属性一样是 继承自prototype对象的属性
//6.3 in运算符
//in运算符可以用来判断,某个实例是否含有某个属性,无论本地属性,还是prototype对象的属性,只要含有就返回true
console.log('name' in cat1); //true
console.log('type' in cat2); //true
console.log('eat' in cat1); //true
//in运算符还可以用来遍历某个对象的所有属性。
for(var prop in cat1) { alert("cat1["+prop+"]="+cat1[prop]); }
for(var prop in cat1){
console.log("cat1["+prop+"]="+cat1[prop]);
}
//cat1[name]=大黄
//cat1[color]=yellow
//cat1[type]=猫科动物
以上只是学习笔记,记录下。
原文地址:http://www.ruanyifeng.com/blog/2010/05/object-oriented_javascript_encapsulation.html
浙公网安备 33010602011771号