[英]“Private” properties inheritance in Javascript
I wanted to structure my javascript application with a modular pattern, as such: 我想用模块化模式构建我的javascript应用程序,如下所示:
APP = (function() {
// Private stuff
var _privateVariable = 'private',
_priv = 'priv'
_privateMethod = function(){ /* */ };
// Exposed API
return {
publicVariable : 'public',
publicMethod : function(){
return _privateVariable
};
}());
Then I want to be able to extend the application through plugin-like modules; 然后我希望能够通过类似插件的模块扩展应用程序; for example, using jQuery:
例如,使用jQuery:
$.extend(true, APP, (function() {
// Child private stuff
var _privateVariable = 'childPrivate',
// Exposed API
return {
}()))
What I am trying to achieve is either one of the following: 我想要实现的是以下任何一种:
APP.publicMethod()
after extending it, I want to return 'childPrivate'
and not 'private'
; APP.publicMethod()
时,我想返回'childPrivate'
而不是'private'
; _priv
from the extended exposed API . _priv
。 In summary, I would like that the private variables defined in the parent module would be inherited in the child module as private members of the child. 总之,我希望父模块中定义的私有变量将作为子模块的私有成员在子模块中继承。
With this structure you can't do that. 有了这种结构,你就无法做到这一点。 Take a look at an awesome post by John Resig on how to achieve inheritance in javascript: http://ejohn.org/blog/simple-javascript-inheritance/
看看John Resig关于如何在javascript中实现继承的精彩文章: http : //ejohn.org/blog/simple-javascript-inheritance/
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.