js创建对象有几种方式

如题所述

第1个回答  2017-02-16
object literal (字面模式)
var car = { horn: function() { alert("du..."); }};// du...car.horn();prototype (原型模式)
function car() { //} car.prototype.horn = function() { alert("du...");}; var camaro = new car();camaro.horn();singleton (单例模式)
var car = new function() { this.horn = function() { alert("du..."); };};// du...car.horn();
object constructor (对象构造器)var car = new Object();car.horn = function() { alert("du...");};// du...car.horn();
function constructor (函数构造器)function car() { this.horn = function() { alert("du..."); };} var camaro = new car();// du...camaro.horn();
Object.create (IE 9+, FF 4+, Chrome 5+, Opera 11.6+, Safari 5+)
function car() { //} car.prototype = { horn: function() { alert("du..."); }}; var camaro = Object.create(car.prototype);// du...camaro.horn();
第2个回答  2017-02-16
._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;脚本"本回答被网友采纳
相似回答