急js创建对象有几种方式,不懂的请不要瞎答谢谢
发布网友
发布时间:2022-05-15 08:00
我来回答
共2个回答
热心网友
时间:2022-05-15 09:29
object literal (字面模式)
var car = {
horn: function() {
alert("...");
}
};
// ...
car.horn();
prototype (原型模式)
function car() {
//
}
car.prototype.horn = function() {
alert("...");
};
var camaro = new car();
camaro.horn();
singleton (单例模式)
var car = new function() {
this.horn = function() {
alert("...");
};
};
// ...
car.horn();
object constructor (对象构造器)var car = new Object();
car.horn = function() {
alert("...");
};
// ...
car.horn();
function constructor (函数构造器)function car() {
this.horn = function() {
alert("...");
};
}
var camaro = new car();
// ...
camaro.horn();
Object.create (IE 9+, FF 4+, Chrome 5+, Opera 11.6+, Safari 5+)
function car() {
//
}
car.prototype = {
horn: function() {
alert("...");
}
};
var camaro = Object.create(car.prototype);
// ...
camaro.horn();
待高手补充、、、
热心网友
时间:2022-05-15 10:47
var obj = new object();
obj.name = '123';
var obj = {name:'123'};
var car = function(){};
var test = new car();