16.javascript中定义对象的几种方式(javascript中没有类的概念,只有对象):
1)基于已有对象扩充其属性和方法:
var object = new Object();
object.name = "zhangsan";
object.sayName = function(name){
this.name = name;
alert(this.name);
}
object.sayName("lisi");
2)工厂方式:
//工厂方式创建对象
function createObject(){
var object = new Object();
object.username = "zhangsan";
object.password = "123";
object.get = function(){
alert(this.username + ", " + this.password);
}
return object;
}
var object1 = createObject();
var object2 = createObject();
object1.get();
//带参数的构造方法
function createObject(username,password){
var object = new Object();
object.username = username;
object.password = password;
object.get = function(){
alert(this.username + ", " + this.password);
}
return object;
}
var object1 = createObject("zhangsan","123");
object1.get();
//让一个函数被多个对象共享,而不是每一个对象拥有一个函数对象
function get(){
alert(this.username + ", " + this.password);
}
function createObject(username,password){
var object = new Object();
object.username = username;
object.password = password;
object.get = get;
return object;
}
var object1 = createObject("zhangsan","123");
var object2 = createObject("lisi","456");
object1.get();
object2.get();
3)构造函数方式:
function Person(){
//在执行第一行代码前,js引擎会为我们生成一个对象
object.username = "zhangsan";
object.password = "123";
object.getInfo = function(){
alert(this.username + ", " + this.password);
}
//此处有一个隐藏的return语句,用于将之前生成的对象返回
}
var person = new Person();
person.getInfo();
//可以在构造对象时传递参数
function Person(username,password){
//在执行第一行代码前,js引擎会为我们生成一个对象
object.username = username;
object.password = password;
object.getInfo = function(){
alert(this.username + ", " + this.password);
}
//此处有一个隐藏的return语句,用于将之前生成的对象返回
}
var person = new Person("zhangsan","123");
person.getInfo();
4)原型 ("prototype")方式:
//使用原型 (prototype)方式创建对象
function Person(){
}
Person.prototype.username = "zhangsan";
Person.prototype.password= "123";
Person.prototype.getInfo = function(){
alert(this.username + ", " + this.password);
}
var person1 = new Person();
var person2 = new Person();
person1.username = "lisi";
person1.getInfo();
person2.getInfo();
//
function Person(){
}
Person.prototype.username = new Array();
Person.prototype.password= "123";
Person.prototype.getInfo = function(){
alert(this.username + ", " + this.password);
}
var person1 = new Person();
var person2 = new Person();
person1.username.push("zhangsan");
person1.username.push("lisi");
person1.password= "456";
person1.getInfo();
person2.getInfo();
//使用原型 + 构造函数方式来定义对象
function Person(){
this.username = new Array();
this.password= "123";
}
Person.prototype.getInfo = function(){
alert(this.username + ", " + this.password);
}
var person1 = new Person();
var person2 = new Person();
person1.username.push("zhangsan");
person2.username.push("lisi");
person1.getInfo();
person2.getInfo();
5)动态原型方式:在构造函数中通过标志量让所有对象共享一个方法,而每个对象拥有自己的属性。
function Person(){
this.username = new Array();
this.password= "123";
if(typeof Person.flag == "undefined"){
alert("invoked");
Person.prototype.getInfo = function(){
alert(this.username + ", " + this.password);
}
Person.flag = true;
}
}
var person1 = new Person();
var person2 = new Person();
person1.getInfo();
person2.getInfo();