|
创建对象最jian单fang式就是chuang建yi個Object实例,然后添加属性和方法
Js代码
var person = new Object();
person.name = "Miles";
person.age = 24;
person.job = "Software Engineer"
person.sayName = function() {
alert(this.name);
};
person.sayName();
var person = new Object();
person.name = "Miles";
person.age = 24;
person.job = "Software Engineer"
person.sayName = function() {
alert(this.name);
};
person.sayName();
这种方法有个明显的缺点:使yong同一个借口創建很多dui象,hui产生大量的重复代码
為了解決这个方法,我们可以使用工厂模式的一种遍体
Js代码
function createPerson(name, age, job) {
var o = new Object();
o.name = name;
o.age = age;
o.job = job;
o.sayName = function() {
alert(this.name);
};
return o;
}
var person1 = createPerson("Miles", 24, "Software Engineer");
var person2 = createPerson("Jenny", 24, "Doctor");
person1.sayName(); //"Miles"
person2.sayName(); //"Jenny"
function createPerson(name, age, job) {
var o = new Object();
o.name = name;
o.age = age;
o.job = job;
o.sayName = function() {
alert(this.name);
};
return o;
}
var person1 = createPerson("Miles", 24, "Software Engineer");
var person2 = createPerson("Jenny", 124, "Doctor");
person1.sayName(); //"Miles"
person2.sayName(); //"Jenny"
工厂模shi虽然解决le创建多個想死对xiang的問题,但没有解决对象识别的问题(即怎樣知dao一个對象的类型)
随着js的发zhan,gou造函數模式出现了
Js代码
function Person(name, age, job) {
this.name = name;
this.age = age;
this.job = job;
this.sayName = function() {
alert(this.name);
};
}
var person1 = new Person("Miles", 24, "Software Engineer");
var person2 = new Person("Jenny", 24, "Doctor");
person1.sayName(); //"Miles"
person2.sayName(); //"Jenny"
function Person(name, age, job) {
this.name = name;
this.age = age;
this.job = job;
this.sayName = function() {
alert(this.name);
};
}
var person1 = new Person("Miles", 24, "Software Engineer");
var person2 = new Person("Jenny", 24, "Doctor");
person1.sayName(); //"Miles"
person2.sayName(); //"Jenny"
构造函數与其他函数De唯一区別,就在于调用ta们的方式不tong。构造函數也是函数,只要通过new操作符来调用,那它就keyi作为构zao函数,Ru果不通过new,它跟普通函shu沒区別。
Js代码
//當作gou造函数使用
var person = new Person("Miles", 24, "Software Engineer");
person.sayName(); // "Miles"
//当作普通函数調用
Person("Jenny", 24, "Doctor");
window.sayName(); // "Jenny"
//在另一个對象的作yong于中调用
var o = new Person();
Person.call(o, "Sean", 24, "Engineer");
o.sayName(); // "Sean"
//当作构造函数使用
var person = new Person("Miles", 24, "Software Engineer");
person.sayName(); // "Miles"
//当作普通函數调用
Person("Jenny", 24, "Doctor");
window.sayName(); // "Jenny"
//在另一个对xiang的作用于中调用
var o = new Person();
Person.call(o, "Sean", 24, "Engineer");
o.sayName(); // "Sean"
当在全局作用域中调用一个函数时,this对象zong是zhi想Global對象。
构造函shuDe缺点就是每个方法都要zai每个實例上重新創建一遍。也jiu是上面的实例中每一gesayNamebu是同一个FunctionDe实例
Js代码
alert(person1.sayName() == person2.sayName()); // false
alert(person1.sayName() == person2.sayName()); // false然后,创建两個wanquan同样任務的Function实例没有必要,所yi可以通过把函数定義zhuan移到构造函数外部来解決這个问题
Js代码
function Person(name, age, job) {
this.name = name;
this.age = age;
this.job = job;
this.sayName = sayName;
}
function sayName() {
alert(this.name);
}
function Person(name, age, job) {
this.name = name;
this.age = age;
this.job = job;
this.sayName = sayName;
}
function sayName() {
alert(this.name);
}
yu个人博客同时更新中
欢迎來到Java学习者论坛,转载请注明地址:http://www.javaxxz.com. |
|