我正在与一位开发人员讨论侵入javascript私有(private)函数是否有意义。备选方案是:一个构造函数和一个包含所有函数的原型(prototype),非API方法(私有(private))将仅使用下划线_function_name命名,以便开发人员知道他们可以调用什么,不能调用什么。API函数的构造函数和原型(prototype),以及在私有(private)命名空间内作为私有(private)函数的自由函数,除了这个命名空间之外,其他命名空间都隐藏了它们。我们不考虑其他方法,例如在varprivate_var=function(){}形式的构造函数中创建私有(private)
有没有办法让模块模式中的公共(public)函数动态访问私有(private)变量?test1显示了我所说的“动态访问”但使用公共(public)变量的意思varx=(function(){varx=0,y=2,z=5;return{toast:123,test1:function(arg){returnthis[arg];},test2:function(){//??}};}());console.log(x.test1("toast"));//123console.log(x.test2("y"));//shouldreturn2我最终创建了一个私有(private)变量(一个对象
在我当前的项目中,我使用的是ExtJs3.3。我创建了许多具有私有(private)变量和函数的类。例如:MyPanel=function(config){config=config||{};varbar='bar';//privatevariablefunctiongetBar(){//publicfunctionreturnbar;}functionfoo(){//privatefunction}Ext.apply(config,{title:'Panel',layout:'border',id:'myPanel',closable:'true',items:[]});MyPane
我在ExtJS论坛上做了一些关于扩展类中的私有(private)方法和字段的研究,我找不到任何真正的答案。当我说扩展类时,我的意思是这样的:Ext.ux.MyExtendedClass=Ext.extend(Ext.util.Observable,{publicVar1:'Variablevisiblefromoutsidethisclass',constructor:function(config){this.addEvents("fired");this.listeners=config.listeners;},//toshowthatIneedtousethebaseclassp
我正在尝试实现RevealingModulePattern,但我无法公开修改后的私有(private)属性。varmyRevealingModule=(function(){varname='Diogo';functionsetName(){name=name+'Cardoso';}return{fullName:name,set:setName};}());//Sampleusage:myRevealingModule.set();console.log(myRevealingModule.fullName);//"Diogo"insteadoftheexcepted"DiogoCa
有没有一种方法可以从类中的“私有(private)”函数调用“公共(public)”javascript函数?查看下面的类:functionClass(){this.publicMethod=function(){alert("hello");}privateMethod=function(){publicMethod();}this.test=function(){privateMethod();}}这是我运行的代码:varclass=newClass();class.test();Firebug给出了这个错误:publicMethod未定义:[出现此错误时中断]publicMeth
我正在修补一些jQuery的Draggable代码*。目标是避免修改原始源文件和动态修补内部功能之一。函数_generatePosition声明如下:(function($){$.widget("ui.draggable",$.ui.mouse,{..._generatePosition:function(event){...}}})(jQuery);是否可以实现动态替换呢?*因此它计算相对于父元素顶部的捕捉网格,而不是相对于被拖动元素的顶部。参见here了解更多详情。 最佳答案 您可以操作单个实例:.draggable().dat
我正在使用Angular4,我正在运行:ngbuild--prod我明白了:ngbuild--prodYourglobalAngularCLIversion(1.2.2)isgreaterthanyourlocalversion(1.0.0).ThelocalAngularCLIversionisused.Todisablethiswarninguse"ngset--globalwarnings.versionMismatch=false".Hash:7fce5d10c4c3ac9745e8Time:68351mschunk{0}polyfills.7790a64cc25c48ae62
我是javascript的新手,我花了一些时间尝试在js中创建命名空间对象。现在,这就是我想要做的:MainObject=function(){varprivateVariable="i'mprivate";varprivateMethod=function(){//doSomething}this.publicMethod=function(){//doPublicSomething}}MainObject.prototype.nested=function(){this.publicNestedMethod=function(){//that'snotworkingatallthi
我可以从私有(private)方法中调用公共(public)方法吗:varmyObject=function(){varp='privatevar';functionprivate_method1(){//canIcallpublicmethod"public_method1"fromthis(private_method1)oneandifyesHOW?}return{public_method1:function(){//dostuffhere}};}(); 最佳答案 做类似的事情:varmyObject=function(){