javascript创建对象简单的说,无非就是使用内置对象或各种自定义对象,当然还可以用JS0N; 但写法有很多种,也能混合使用
对象字面量的方式
person={firstname: "Mark", last name: "Yun",age: 25, eyecolor: "black"};
用function来模拟无参的构造函数
function Person () {}
var person=new Person();〃定义一个 function,如果使用 new"实例化",该 function 可以看作是一个 Class
person. name="Mark";
person. age="25";
person. work=function () {
alert (person. name+" hello…");
}
person. work ();
用function来模拟参构造函数来实现(用this关键字定义构造的上下文属性)
function Pet(name,age,hobby){
this. name=name;//this 作用域:当前对象
this. age=age;
this. hobby=hobby;
this. eat=function() {
alert ("我叫"+this. name+",我喜欢"+this. hobby+",是个程序员");
}
}
var maidou =new Pet ("麦兜",25, "coding") ;//实例化、创建对象
maidou. eat ();//调用 eat 方法
用工厂方式来创建(内置对象)
var wcDog =new Object();
wcDog. name="旺财";
wcDog. work=function() {
alert ("我是"+wcDog. name+",汪汪汪 ");
}
wcDog. work ();
用原型方式来创建
function Dog() {
}
Dog. prototype, name="旺财";
Dog. prototype. eat=function() {
alert (this. name+"是个吃货");
}
var wangcai =new Dog();
wangcai. eat ();
用混合方式来创建
function Car(name,price) {
this. nam=name;
this. price=price;
}
Car. prototype. se11=function () {
alert ("我是"+this. name+",我现在卖"+this. price+"万元");
}
var camry =new Car("凯美瑞",27);
camry. sell ();
Was this helpful?
0 / 0