在Javascript中限制main对象对其属性的属性的访问
Restrict access of the main obj to the properties of its properties in Javascript
好的,我正在用javascript创建一个链接方法,我试图存档主对象或类可以访问4个属性,这些属性是函数和属性必须访问主对象不能访问的一些函数。
这里有一个例子:
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()
这在javascript中可能吗?记住必须是可链接的
我仍然不太确定你在问什么,但这是我想到的。我做了两个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/
Car将不能访问Owner的名字,除非它先通过Owner。
相关文章:
- JavaScript Pub/Sub属性访问问题
- AngularJS:如何用同一对象的另一个属性访问一个属性
- Ember JS属性访问器
- 对方法或属性访问的意外调用
- 对jQuery中IE8的方法或属性访问的意外调用
- 对方法或属性访问的意外调用.在JQUERY
- XDomainRequest onload上对方法或属性访问的意外调用
- Javascript属性访问速度:var.properties与var[“属性”]的区别
- 从函数属性访问对象属性;不适用于Firefox
- 重写内部 HTML 属性访问
- (仅在IE中)SCRIPT65535:对方法或属性访问的意外调用.主.js,第 152 行字符 28
- 使用表达式属性访问不同的模型模板选项
- jQuery mobile 1.1.0 滑块:JS-对象属性访问
- Ractive.js,通过命名属性访问父属性
- jQuery验证插件addMethod可以通过属性访问
- 如何使用动态属性访问对象
- 我无法通过 Firefox 中的“for”属性访问标签,但可以在 Chrome 中访问标签
- JavaScript 对象原型属性访问
- 使用虚拟属性访问猫鼬模型中的嵌套文档
- 是否可以在Javascript/Coffeescript中的属性访问期间自动调用函数调用