2012-12-20 48 views
1

我dindn't知道一个更好的标题,所以要解释它之间的原始值, 可以说你有一个“构造”,这部分继承 - 分享对象

  • 实例化一个对象,并设置一些属性
    • 在创建annother对象Instatiation的过程
    • 这个对象的原型应该阴影的一些属性从第一对象给他的孩子

所以当propertie num第一对象改变其他对象样机propertie num 也应该被

改变,这将在num案件过程中工作是

  • 包裹在一个对象
  • 非原始对象的特性/元素

但如果num将是一个数或字符串

如果num将在第一对象作为原始瓦尔作为值传递,而不是通过引用,或者如果propertie被重写的样机的propertie不会改变是一个对象,将与新的对象覆盖

所以我的问题是 是那里可以让一个对象从另一个对象继承属性的原始值,并让他们分享一个参考任何“整齐”的方式?

下面是一些示例代码,你可以跳过第一个,它在这里的代码

/* Inheritance Helper*/ 

var base = (function baseConstructor() { 

    var obj = { 
    create:function instantiation() { 
     if(this != base) { 
     var instance = Object.create(this.pub); 
     this.init.apply(instance,arguments); 
     this.instances.push(instance); 
     return instance; 
     } else { 
      throw new Error("You can't create instances of base"); 
     } 
    }, 
    inherit:function inheritation() { 
     var sub = Object.create(this); 
     sub.pub = Object.create(this.pub); 
     sub.sup = this; 
     return sub; 
    }, 
    initclosure:function initiation() {}, 
    instances: [], 
    pub: {} 

    }; 



    Object.defineProperty(obj,"init",{ 
    set:function (fn) { 
    if (typeof fn != "function") 
     throw new Error("init has to be a function"); 
    if (!this.hasOwnProperty("initclosure"))  
     this.initclosure = fn; 
    }, 
    get:function() { 
     var that = this; 
     //console.log(that) 
      return function() { 
       if(that.pub.isPrototypeOf(this)) //!(obj.isPrototypeOf(this) || that == this)) 
       that.initclosure.apply(this,arguments); 
       else 
       throw new Error("init can't be called directly"); 
      }; 
    }  

    }); 


    Object.defineProperty(obj,"create",{configurable:false,writable:false}); 
    Object.defineProperty(obj,"inherit",{configurable:false,writable:false}); 
    return obj; 
})(); 

/*Helpers*/ 
function merge (obj) { 
if(arguments.length < 2) 
    throw new Error({msg:"At least 2 parameters needed"}); 
    for (var i = 1, ilen = arguments.length;i < ilen; i++) 
    for (var k in arguments[i]) 
    obj[k] = arguments[i][k]; 
} 

/*Helpers for workarounds*/ 
function tieProp (prop,obj) { 
    if(arguments.length < 3) 
    throw new Error({msg:"At least 2 Objects are needed"}); 
    var ref = obj[prop]; 

    for (var i = 1,ilen = arguments.length;i<ilen;i++) 
     Object.defineProperty(arguments[i],prop,{ 
     set: function (val) { 
     ref = val; 
     }, 
     get: function() { 
     return ref; 
     } 
    }); 

} 

这样的完整性,这是其中的对象被创建的部分

/*Example Code*/ 

var Series = base.inherit(); 
Series.init = function (specs) { 
    var _Series = this; 
     specs = specs ||{}; 

    this.seasons = []; 

    var Season = Series.inherit(); 
    Season.init = function(specs) { 
    var _Season = this; 
     specs = specs || {}; 
    _Series.seasons.push(this); 

    merge(this,specs); 



    }; 

    merge(this,specs); 
    Season.pub.score = this.score; // First way 
    Season.pub.stats = this.stats; // Second Way 
    tieProp("scoreTied",this,Season.pub); //Third Way 
    Season.pub.scoreSetter = this.scoreSetter; // Second Way 

    this.updateScore = function (score) { // Forth Way 
     this.scoreSetter = score; 
     Season.pub.scoreSetter = score; 
    }; 
    tieProp("someObj",this,Season.pub); //Obj Example 

    this.addSeason = function (specs) { 
    Season.create(specs); 
    }; 


}; 
Series.pub.toString = function() { 
return this.title + " has a score of " + this.scoreTied ; 
}; 


var Futurama = Series.create({ 
    title:"Futurama", 
    score:1, // 1. 
    scoreTied:2, // 2. 
    stats:{ //3. 
    score:3 
    }, 
    scoreSetter:4, 
    someObj:{a:"b"} 
}); 
Futurama.addSeason(); 

并让我们在更改属性之前记录控制台输出

console.log("BeforeChange",Futurama.score + " - " + Futurama.seasons[0].score); //"1 - 1" 
console.log(Futurama.scoreTied + " - " + Futurama.seasons[0].scoreTied); // "2 - 2" 
console.log(Futurama.stats.score + " - " + Futurama.seasons[0].stats.score); // "3 - 3" 
console.log(Futurama.scoreSetter + " - " + Futurama.seasons[0].scoreSetter); //"4 - 4" 
console.log(JSON.stringify(Futurama.someObj) + " - " + JSON.stringify(Futurama.seasons[0].someObj)); //"{"a":"b"} - {"a":"b"}" 

然后使用

  • 对象既可以当改变Futurama

    Futurama.score = 2; //TFirst way // This will fail 
    Futurama.scoreTied = 3; //Second way 
    Futurama.stats.score = 4; // Third way 
    Futurama.updateScore(5); // Forth Way 
    Futurama.someObj = {b:"a"}; // Object replacement 
    

    比分性能和日志他们

    console.log("After Change",Futurama.score + " - " + Futurama.seasons[0].score); // 2 - 1 
    console.log(Futurama.scoreTied + " - " + Futurama.seasons[0].scoreTied); // 3 - 3 
    console.log(Futurama.stats.score + " - " + Futurama.seasons[0].stats.score); //4 -4 
        console.log(Futurama.scoreSetter + " - " + Futurama.seasons[0].scoreSetter); //5 - 5 
    console.log(JSON.stringify(Futurama.someObj) + " - " + JSON.stringify(Futurama.seasons[0].someObj)) ; //"{"b":"a"} - {"b":"a"}" 
    

    所以,这将是可能的。defineProperty提供的属性与getter和setter

function tieProp (prop,obj) {...

但如果使用Object.defineProperty会在这种情况下适当的,我不知道我是不是真的有设置属性描述符以让某些属性共享一个对原始值的引用?

  • 结束语中会引用传递Object的所有原始值,并改变这个对象propertie

Season.pub.stats = this.stats; // Second Way

这将是确定的,但我不认为舒服,因为我不得不把物业搬到另一个地方,这种拿走了一些命名的自由度,在这个例子中,我想score作为Futurama的得分在Futurama.score而不是Futurama.stats.score

*写作setter方法的属性,这只是设置对象

*的两个值就像this.updateScore = function (score) { // Forth Way *

但我宁愿要从此远离,因为我将不得不增加方法对象

我不知道我是不是应该这样做,或者如果我只是错过了一个非常简单的方法做到这一点?

在正确的方向有任何建议或勾缝将非常感激

,并在此先感谢答案和耐心通过这个

继承人JSBin念给摆弄

+0

+1把这么多的细节到您的问题! –

回答