2013-06-12 30 views
0

好吧,我正在创建一个JavaScript链接方法,我试图归档主要的obj或类有权访问4个属性,这些属性是函数和该属性必须能够访问主obj无法访问的一些函数。限制对主要obj对其属性的Javascript属性的访问

下面一个例子:

var Main = function(){ 

return { 
property1:function(){ 
return this; 
}, 
property2:function(){ 
return this;  
}, 
etc:function(){ 
    return this; 
}... 

} 
} 

要执行,你知道它是这样的:

Main().property1().property2().etc(); 

主要访问它的属性,但Main不能访问这个特性是属性Main的物业。以更简单的方式:just the properties of Main must have access, not Main

下面的例子:

Main().property().innerProperty1().innerProperty2().etc()//cool, property1 can access to innerProperty 1 and 2 and etc() 

,但如果我想这样做:

Main().innerProperty() // ERROR, Main does not have acccess to innerProperty() 

莫非在的javascrip可能吗?请记住,必须是可链接的。

+0

我对你的例子感到困惑,因为你对每个“属性”所做的只是返回Main,所以它将有权访问Main可访问的所有属性。但是链接本身有什么意义呢? – Tony

+0

因为如果主要访问将具有缺乏意义的内部属性。所以Main不能访问它1:由于缺乏意义和2,因为内部属性不会以任何方式影响Main – Ligth

回答

0

我还不是很确定你在问什么,但这是我想出来的。我做了两个JS类来演示你在说什么。

function Owner(name) { 
    this.Name = name; 

    this.ChangeName = function (newName) { 
     this.Name = newName; 
     return this; 
    }; 
} 


function Car(make, model, owner) { 

    this.Make = make; 
    this.Model = model; 
    this.Owner = owner; 

    this.UpdateMake = function (newMake) { 
     this.Make = newMake; 
     return this; 
    }; 

    this.UpdateModel = function (newModel) { 
     this.Model = newModel; 
     return this; 
    }; 

    this.UpdateOwner = function (newOwner) { 
     this.Owner = newOwner; 
     return this; 
    }; 

} 

这里的小提琴:http://jsfiddle.net/whytheday/zz45L/18/

车将不能访问用户名称,除非它通过业主先去了。