要使用公共方法创建JavaScript类,我需要执行以下操作:
function Restaurant() {}
Restaurant.prototype.buy_food = function(){
// something here
}
Restaurant.prototype.use_restroom = function(){
// something here
}
这样,我班的用户可以:
var restaurant = new Restaurant();
restaurant.buy_food();
restaurant.use_restroom();
如何创建可以由buy_food
和use_restroom
方法调用但不能由该类的用户外部调用的私有方法?
换句话说,我希望我的方法实现能够做到:
Restaurant.prototype.use_restroom = function() {
this.private_stuff();
}
但这不起作用:
var r = new Restaurant();
r.private_stuff();
如何将private_stuff
定义为私有方法,使两者都成立?
我已经读过Doug Crockford的文章几次,但似乎公共方法不能调用“私有”方法,而外部可以调用“特权”方法。
#1楼
由于每个人都在这里发布自己的代码,所以我也要这样做...
我喜欢Crockford,因为他在Javascript中引入了真正的面向对象模式。 但是他还提出了一个新的误解,即“那个”。
那么,为什么他要使用“ that = this”呢? 它与私有功能完全无关。 它与内部功能有关!
因为根据Crockford,这是错误的代码:
Function Foo( ) {
this.bar = 0;
var foobar=function( ) {
alert(this.bar);
}
}
因此,他建议这样做:
Function Foo( ) {
this.bar = 0;
that = this;
var foobar=function( ) {
alert(that.bar);
}
}
因此,正如我所说,我很确定Crockford对他的解释是错误的,但是他的代码肯定是正确的。 还是他只是在欺骗Javascript世界,以了解谁在复制他的代码? 我不知道...我不是浏览器极客; D
编辑
嗯,这就是全部内容: 'var that = this;'是什么? 在JavaScript中意味着什么?
因此Crockie的解释确实错了。。。。但是他的代码正确,所以他仍然是个好人。 :))
#2楼
我想到了这一点:编辑:实际上,有人链接到相同的解决方案。 h!
var Car = function() {
}
Car.prototype = (function() {
var hotWire = function() {
// Private code *with* access to public properties through 'this'
alert( this.drive() ); // Alerts 'Vroom!'
}
return {
steal: function() {
hotWire.call( this ); // Call a private method
},
drive: function() {
return 'Vroom!';
}
};
})();
var getAwayVechile = new Car();
hotWire(); // Not allowed
getAwayVechile.hotWire(); // Not allowed
getAwayVechile.steal(); // Alerts 'Vroom!'
#3楼
如果要使用公共功能访问私有功能的所有公共和私有功能,请为这样的对象布局代码:
function MyObject(arg1, arg2, ...) {
//constructor code using constructor arguments...
//create/access public variables as
// this.var1 = foo;
//private variables
var v1;
var v2;
//private functions
function privateOne() {
}
function privateTwon() {
}
//public functions
MyObject.prototype.publicOne = function () {
};
MyObject.prototype.publicTwo = function () {
};
}
#4楼
通常,我将私有Object _临时添加到该对象。 该方法必须在“电源构造器”中完全打开隐私。 如果从原型中调用方法,则将能够覆盖原型方法
使“ Power-constructor”中的公共方法可访问:(ctx是对象上下文)
ctx.test = GD.Fabric.open('test', GD.Test.prototype, ctx, _); // is a private object
现在我有了这个openPrivacy:
GD.Fabric.openPrivacy = function(func, clss, ctx, _) { return function() { ctx._ = _; var res = clss[func].apply(ctx, arguments); ctx._ = null; return res; }; };
#5楼
您必须对实际的构造函数进行封闭,在此可以定义私有方法。 要通过这些私有方法更改实例的数据,您必须将它们与函数一起使用,或者通过使用.apply(this)调用此函数来给它们“ this”:
var Restaurant = (function(){
var private_buy_food = function(that){
that.data.soldFood = true;
}
var private_take_a_shit = function(){
this.data.isdirty = true;
}
// New Closure
function restaurant()
{
this.data = {
isdirty : false,
soldFood: false,
};
}
restaurant.prototype.buy_food = function()
{
private_buy_food(this);
}
restaurant.prototype.use_restroom = function()
{
private_take_a_shit.call(this);
}
return restaurant;
})()
// TEST:
var McDonalds = new Restaurant();
McDonalds.buy_food();
McDonalds.use_restroom();
console.log(McDonalds);
console.log(McDonalds.__proto__);
来源:oschina
链接:https://my.oschina.net/stackoom/blog/3161298