一、使用字面量创建对象
实例1
var person = {firstName:"Bill", lastName:"Gates", age:62, eyeColor:"blue"};
实例2
var person = {
firstName:"Bill",
lastName:"Gates",
age:62,
eyeColor:"blue"
};
二、使用 JavaScript 关键词 new创建对象
var person = new Object();
person.firstName = "Bill";
person.lastName = "Gates";
person.age = 50;
person.eyeColor = "blue";
三、使用构造器函数创建对象原型
function person(firstName, lastName, age, eyeColor) {
this.firstName = firstName;
this.lastName = lastName;
this.age = age;
this.eyeColor = eyeColor;
this.changeName = function (name) {
this.lastName = name;
};
}
var myFather = new Person("Bill", "Gates", 62, "blue");//根据对象原型,创建实例对象myFather
var myMother = new Person("Steve", "Jobs", 56, "green");//根据对象原型,创建实例对象myMother
以上就是本文的全部内容,感谢大家支持JScript之家——编程学习者社区!