0

好的,我正在使用 javascript 创建一个链接方法,并且我正在尝试归档主 obj 或类可以访问 4 个作为函数的属性,并且这些属性必须可以访问一些主 obj 无法访问的函数。

这里有一个例子:

var Main = function(){

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

}
}

如您所知,执行如下:

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

Main 可以访问其属性,但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中可能吗?请记住,它必须是可链接的。

4

1 回答 1

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/

除非先通过所有者,否则汽车将无法访问所有者的姓名。

于 2013-06-12T20:48:49.760 回答