js创建对象有几种方式
发布网友
发布时间:2022-04-20 02:58
我来回答
共2个回答
热心网友
时间:2022-04-07 06:14
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-04-07 07:32
._lev=true;
if(typeof Parent.name;
var x =new Parent(),原型方式
function Parent(){
this;;){
Parent;
this;
}
Parent.lev=function(){
return this.lev()).age=4.lev=function(){
return this.name=".name混合的构造函数;
}
}._lev=="脚本".name="
alert(x.prototype;
this;;
};
};
动态原型方式
function Parent(){
this.lev()).age=4;
Parent;undefined"
var x =new Parent().prototype;
alert(x;脚本"