使用事构造函数进行的创建对象
functionPerson(username,password)
{
//在执行第一行代码之前,js引擎会为我们生成一个对象
this.username= username;
this.password= password;
this.getInfo= function()
{
alert(this.username+"''"+this.password);
};
//此处有一个隐藏的return语句,用于将之前的对象返回。
}
varperson = new Person("zhangsan","123");
person.getInfo();
//这是利用prototype进行创建对象
//在这里需要知道的就是当你需要创建对象的时候function后面的引用名字的首字母是大写的
//这样创建对象不能通过传递参数的方式来给属性赋值,和指定方法
functionPerson()
{
}
Person.prototype.username ="zhangsan";
Person.prototype.password = "123";
Person.prototype.info = function()
{
alert(this.username + "," + this.password);
};
var person1 = new Person();
person1.username = "lisi";
person1["password"] ="123456";
//这两种方式都是在进行设置属性
person1.info();//得到是lisi和123456
var person2 = new Person();
person2.info();//得到的还是zhangsan和123
//原因是以上两个对象虽然共享同一份属性但是,由于对person1对属性的更改就是这个属性的对应值改变所以没有怼
//person2没有改变
function Person()
{
}
Person.prototype.username = new Array();
Person.prototype.password = "123";
Person.prototype.info = function()
{
alert(this.username + "," + this.password);
};
var person = new Person();
var person2 = new Person();
person.username.push("zhangsan");
person["password"] ="123456";
//这两种方式都是在进行设置属性
person.info();//得到的是zhangsan和123456
person2.info();//得到是zhangsan和123
//这个和上面有区别是因为这个username对应的是一个数组,数组是一个对象,所以在这里所以person对象的这个对应的数组改变了,那么自然
//person2也会改变,但是password这个属性就不一样。
//利用的protetype和构造函数一块创建对象
function Person()
{
this.username = new Array();
this.password = "123";
}
Person.prototype.info = function()
{
alert(this.username+","+this.password);
};
var person = new Person();
var person1 = new Person();
person.username.push("zhangsan");
person.password = "456";
person.info();//输出的是zhangsan和456
person1.info();//输出的是空和123,
//这样属性就是每个对象都各自有一份是独立的,方法是共享的,在这里由于方法的使用都是每个调用这个方法的那个对象(this),所以肯定也是互不干扰的
functionPerson()
{
this.username = "zhangsan";
this.password = "123";
if(typeof Person.flag == "undefined")
{
alert("invoke");
// this.info = function()//这样就不行,这样就只能为首次能够进入这个if的对象创建方法,所以在后面的那个创建对象就没有这个方法,因为那个对象进不去这个if语句
Person.prototype.info = function()//这样的好处就是相当于是创建了一个所有对象共同拥有的方法,所以之后那个对象创建的时候虽然不会进入if语句,但是还是可以使用这个函数
{
alert(this.username+","+this.password);
};
Person.flag = true;
}
}
var person = new Person();
var person2 = new Person();
//当创建第一个对象的时候就会进去if判断,这样这个时候就会将Person.flag设置为true,并且创建一个函数
//当这个对象创建完成返回到person的时候,再去创建创建对象的时候,这个if就不成立。这样后方法就不会再去创建
//这样就使得不管创建多少个对象,这个方法只会创建一次。
person.info();
person2.info();
<scripttype="text/javascript">
//第一种继承:对象冒充
functionParent(username)
{
this.username= username;
this.sayHello= function()
{
alert(this.username);
};
}
functionChild(username,password)
{
this.method= Parent;//child中的这个method属性赋了一个Parent函数对象
this.method(username);
//在这里就是指明在调用的时候Parent中的this.username= username;此时this指代的是child,所以这里是将Parent里面的username
//属性的值赋给child中的username属性
//
deletethis.method;
//这三行实现的是从Parent中继承username属性
this.password= password;
this.sayWorld= function()
{
alert(this.username+","+this.password);
};
}
varparent = new Parent("zhangsan");
varchild = new Child("lisi","123");
parent.sayHello();
child.sayHello();
child.sayWorld();//输出zhangsan lisi lisi,123
</script>
//这个是使用call()实现继承
functionParent(username)
{
this.username= username;
this.sayHello= function()
{
alert(this.username);
};
}
functionChild(username,password)
{
Parent.call(this,username);
this.password= password;
this.sayworld= function()
{
alert(this.password);
};
}
varparent = new Parent("zhangsan");
var child = newChild("lisi","123");
parent.sayHello();
child.sayHello();
child.sayworld();
//使用call的一种简单的方法
function test(str,str2)
{
alert(this.username+","+str+str2);
}
varobject = new Object();
object.username= "zhangsan";
test.call(object,"shengsiyuan","lisi");//意思是继承了object,那自然父类的username也是自己的
<scripttype="text/javascript">
function Parent(username)
{
this.username = username;
this.sayHello = function()
{
alert(this.username);
};
}
function Child(username,password)
{
Parent.apply(this, newArray(username));
this.password = password;
this.sayWorld = function()
{
alert(this.password);
};
}
var parent = new Parent(123);
// 在这里只能传递字符串,或者number类型的
var child = newChild("lisi","123");
parent.sayHello();
child.sayHello();
child.sayWorld();
</script>