我有这门课:functionctest(){this.var1="haha";this.func1=function(){alert(this.var1);func2();alert(this.var1);}varfunc2=function(){this.var1="huhu";}}并称它为:varmyobj=newctest();myobj.func1();不是应该第二个警报会弹出“huhu”吗?func2是私有(private)的,它不能访问var1公共(public)变量吗?如果私有(private)函数不能访问公共(public)变量,我该怎么做?提前致谢!
关闭。这个问题是opinion-based.它目前不接受答案。想要改进这个问题?更新问题,以便editingthispost可以用事实和引用来回答它.关闭8年前。Improvethisquestion所以我想要一个不可变的Vector类。为此,我需要一个用于x和y坐标的公共(public)getter和一个私有(private)setter,以便我可以在构造函数中实际初始化这些值。我有几个可供选择的选项,所以我想知道哪个符合惯例。我可以这样做:classVector{constructor(private_x:number,private_y:number){}publicgetx()
我正在尝试更深入地掌握原型(prototype)继承和类创建(我知道,还有其他方法,但为此我正在尝试掌握原型(prototype)。)我的问题是:使用以下代码例如,有没有一种方法可以在Tree和Fruit中创建私有(private)变量,这些变量不会随函数返回,但原型(prototype)函数genus仍然可以访问和批量?varTree=function(name,size){this.name=name;this.size=size;};Tree.prototype.genus=function(){return((typeofthis.name!=='undefined')?thi
我已经阅读了很多关于模块模式的文章。好的,它带来了结构、私有(private)方法等……但是使用下面的代码我可以在不使用它的情况下获得相同的行为。functionHuman(){//privatepropertiesvar_name='';var_age=0;//privatemethodsfunctioncreated(){console.log("Human"+_name+"called");};//publicthis.setName=function(name){_name=name;created();};}varh1=newHuman();h1.setName("John"
在MDC上找到了这个,但是如果我想将一个私有(private)变量添加到vardataset={tables:{customers:{cols:[/*here*/],rows:[/*here*/]},orders:{cols:[/*here*/],rows:[/*here*/]}},relations:{0:{parent:'customers',child:'orders',keyparent:'custid',keychild:'orderid',onetomany:true}}}按照我对Javascript中OOP的理解,如果存在这样的项目,我可以访问dataset.tables
我开始编写一些javascript测试,并试图找出检查模块构造函数的私有(private)成员的最佳方法。例如,在下面的示例中,我使用揭示模块模式将公共(public)api公开到我的模块。我想测试在$.getJSONajax请求的回调期间是否正确设置了privateVar。第二个测试it('shouldupdateprivateVar',...),不起作用,因为myModule.privateVar(有意)不在公共(public)api中对于模块。所以,我的问题是,在不必将privateVar设为公共(public)api的一部分的情况下测试这种行为的最佳方法是什么?是否有更好的方法
我在Javascript中使用模块模式将我的公共(public)接口(interface)与私有(private)实现分开。为了简化我正在做的事情,我的代码生成了一个图表。该图表由多个部分(轴、标签、绘图、图例等)组成。我的代码如下所示:varGraph=function(){varprivate_data;functiondraw_legend(){...}functiondraw_plot(){...}functionhelper_func(){...}...return{add_data:function(data){private_data=data;},draw:functi
假设我有这个匿名函数:(function(window){varprivate='privatemsg';functionsayit(){alert(private)//works}document.body.onclick=sayit;//works})(window);//privateshouldn'tbeaccessiblehere这就是JavaScript的行为方式吗?也就是说,没有办法从匿名函数之外的任何地方访问private?如果是这样,是否有可能找到某种hack从外部访问private,让代码保持原样? 最佳答案 是
考虑以下代码:functionA(){}A.prototype.go=function(){console.log(this);//A{go=function()}varf=function(){console.log(this);//Window};f();}vara=newA();a.go();为什么函数“f”中的“this”指的是全局范围?为什么它不是函数“A”的范围? 最佳答案 JavaScript对特殊名称this指的是什么有不同的概念比大多数其他编程语言做的。恰好有五个不同this的值可以在语言中绑定(bind)的方式。
我想包含一个自定义文件作为Bower依赖项之一。我有以下bower.json{"name":"xyz","version":"0.0.0","dependencies":{"sass-bootstrap":"~2.3.0","requirejs":"~2.1.4","modernizr":"~2.6.2","jquery":"~1.9.1","beautify":"file:/path/to/beautify.js"},"devDependencies":{}}但是当我安装bower时它给出了错误:bowerbeautify#*ENOTFOUNDPackagefile:/path/to