整理一些关于js的面向对对象的资料。
初探
//我们可以看到, 其用function来做class。
var Person = function(name, email, website){
this.name = name;
this.email = email;
this.website = website;
this.sayHello = function(){
var hello = "Hello, I'm "+ this.name + ", \n" +
"my email is: " + this.email + ", \n" +
"my website is: " + this.website;
alert(hello);
};
};
var chenhao = new Person("Chen Hao", "haoel@hotmail.com",
"http://coolshell.cn");
chenhao.sayHello();
要删除对象的属性,很简单:
delete chenhao['email']
- Javascript的数据和成员封装很简单。没有类完全是对象操作。纯动态!
- Javascript function中的this指针很关键,如果没有的话,那就是局部变量或局部函数。
- Javascript对象成员函数可以在使用时临时声明,并把一个全局函数直接赋过去就好了。
- Javascript的成员函数可以在实例上进行修改,也就是说不同实例相同函数名的行为不一定一样。
属性配置 – Object.defineProperty
先看下面的代码:
//创建对象
var chenhao = Object.create(null);
//设置一个属性
Object.defineProperty( chenhao,
'name', { value: 'Chen Hao',
writable: true,
configurable: true,
enumerable: true });
//设置多个属性
Object.defineProperties( chenhao,
{
'email' : { value: 'haoel@hotmail.com',
writable: true,
configurable: true,
enumerable: true },
'website': { value: 'http://coolshell.cn',
writable: true,
configurable: true,
enumerable: true }
}
);
下面就说说这些属性配置是什么意思。
- writable:这个属性的值是否可以改。
- configurable:这个属性的配置是否可以改。
- enumerable:这个属性是否能在for…in循环中遍历出来或在Object.keys中列举出来,默认是不同被列举。
- value:属性值。
- get()/set(_value):get和set访问器。
Get/Set 访问器
关于get/set访问器,它的意思就是用get/set来取代value(其不能和value一起使用),示例如下:
var age = 0;
Object.defineProperty( chenhao,
'age', {
get: function() {return age+1;},
set: function(value) {age = value;}
enumerable : true,
configurable : true
}
);
chenhao.age = 100; //调用set
alert(chenhao.age); //调用get 输出101(get中+1了);
我们再看一个更为实用的例子——利用已有的属性(age)通过get和set构造新的属性(birth_year):
Object.defineProperty( chenhao,
'birth_year',
{
get: function() {
var d = new Date();
var y = d.getFullYear();
return ( y - this.age );
},
set: function(year) {
var d = new Date();
var y = d.getFullYear();
this.age = y - year;
}
}
);
alert(chenhao.birth_year);
chenhao.birth_year = 2000;
alert(chenhao.age);
上面的定义过程也可以写成下面这样
var chenhao = {
name: "Chen Hao",
email: "haoel@hotmail.com",
website: "http://coolshell.cn",
age: 100,
get birth_year() {
var d = new Date();
var y = d.getFullYear();
return ( y - this.age );
},
set birth_year(year) {
var d = new Date();
var y = d.getFullYear();
this.age = y - year;
}
};
alert(chenhao.birth_year);
chenhao.birth_year = 2000;
alert(chenhao.age);
是的,你的确可以这样的,不过通过defineProperty()你可以干这些事:
1)设置如 writable,configurable,enumerable 等这类的属性配置。
2)动态地为一个对象加属性。比如:一些HTML的DOM对像。
继承 和 重载
通过上面的那些示例,我们可以通过Object.create()来实际继承,请看下面的代码,Student继承于Object。var Person = Object.create(null);
Object.defineProperties
(
Person,
{
'name' : { value: 'Chen Hao'},
'email' : { value : 'haoel@hotmail.com'},
'website': { value: 'http://coolshell.cn'}
}
);
Person.sayHello = function () {
var hello = "<p>Hello, I am "+ this.name + ", <br>" +
"my email is: " + this.email + ", <br>" +
"my website is: " + this.website;
document.write(hello + "<br>");
}
var Student = Object.create(Person);
Student.no = "1234567"; //学号
Student.dept = "Computer Science"; //系
//使用Person的属性
document.write(Student.name + ' ' + Student.email + ' ' + Student.website +'<br>');
//使用Person的方法
Student.sayHello();
//重载SayHello方法
Student.sayHello = function (person) {
var hello = "<p>Hello, I am "+ this.name + ", <br>" +
"my email is: " + this.email + ", <br>" +
"my website is: " + this.website + ", <br>" +
"my student no is: " + this. no + ", <br>" +
"my departent is: " + this. dept;
document.write(hello + '<br>');
}
//再次调用
Student.sayHello();
//查看Student的属性(只有 no 、 dept 和 重载了的sayHello)
document.write('<p>' + Object.keys(Student) + '<br>');
通用上面这个示例,我们可以看到,Person里的属性并没有被真正复制到了Student中来,但是我们可以去存取。这是因为Javascript用委托实现了这一机制。其实,这就是Prototype,Person是Student的Prototype。
Prototype
所有的对象都可以有prototype, prototype自己也是对象,那么他也可以有prototype,这样循环下去就形成了一个prototype链,这个链当他遇到链中队形的prototype是null时中止。(Object的默认的prototype是null)
读操作会读取在obj自己和prototype 链上发现的第一个同名属性值
写操作会为obj对象本身创建一个同名属性(如果这个属性名不存在)
这就意味着objectRef.testNumber = 3会在objectRef对象上创建一个property,名字是testNumber,当下一次在要读取testNumber时
propertype链就不会工作,仅仅会得到objectRef的property 3,而MyObject1的testNumber属性并不会被修改。
/* 构建MyObject1这个类型的构造函数
MyObject1 - type.
*/
function MyObject1(formalParameter){
/* 为者对象创建一个属性名字叫testNumber
*/
this.testNumber = formalParameter;
}
/* 构建MyObject2这个类型的构造函数
MyObject2 - type:-
*/
function MyObject2(formalParameter){
/* 为者对象创建一个属性名字叫testString*/
this.testString = formalParameter;
}
/* 下一步的操作会用MyObject1对象替换掉MyObject2默认的prototype属性*/
var obj1 = new MyObject1( 8 );
MyObject2.prototype = obj1;
/* 最后我们创建MyObject2类型的一个对象*/
var objectRef = new MyObject2( "String_Value" );
alert(objectRef.testNumber);
objectRef.testNumber = 5;
alert(objectRef.testNumber);
alert(obj1.testNumber);
通过prototype进行继承
下面的示例是创建一个Student来继承Person。
function Person(name, email, website){
this.name = name;
this.email = email;
this.website = website;
};
Person.prototype.sayHello = function(){
var hello = "Hello, I am "+ this.name + ", <br>" +
"my email is: " + this.email + ", <br>" +
"my website is: " + this.website;
return hello;
};
function Student(name, email, website, no, dept){
var proto = Object.getPrototypeOf;
proto(Student.prototype).constructor.call(this, name, email, website);
this.no = no;
this.dept = dept;
}
// 继承prototype
Student.prototype = Object.create(Person.prototype);
//重置构造函数
Student.prototype.constructor = Student;
//重载sayHello()
Student.prototype.sayHello = function(){
var proto = Object.getPrototypeOf;
var hello = proto(Student.prototype).sayHello.call(this) + '<br>';
hello += "my student no is: " + this. no + ", <br>" +
"my departent is: " + this. dept;
return hello;
};
var me = new Student(
"Chen Hao",
"haoel@hotmail.com",
"http://coolshell.cn",
"12345678",
"Computer Science"
);
document.write(me.sayHello());