草庐IT

private-constructor

全部标签

javascript - 原型(prototype)继承。 obj->C->B->A,但 obj.constructor 是 A。为什么?

varprint=function(text){document.write(text);document.write("");}varA=function(){}A.prototype.name="A";varB=function(){}B.prototype=newA();B.prototype.name="B";varC=function(){}C.prototype=newB();C.prototype.name="C";obj=newC();print(obj.name);print(obj.constructor.prototype.name);print(obj.cons

javascript - 全局和本地以及私有(private)函数 (Javascript)

我目前正在阅读Pragmatic的一本关于Javascript的书,我对一件事感到困惑。他们有一节介绍如何使变量成为全局变量、局部变量或私有(private)变量。局部变量和私有(private)变量有什么区别?有吗?如何使一个变量成为全局变量或局部变量,他们说要在它前面加上'var=',但它非常含糊。 最佳答案 无,人们使用“private”是因为他们弄错了,意思是说“local”局部变量定义为varfoo="local";全局变量是全局作用域对象(在浏览器中是window)的一个属性window.foo="global";您可以

javascript - Controller 函数中的 AngularJS 私有(private)变量

我是Angularjs的新手。我在网上看到一个例子,这让我很困惑。这是代码:angular.module("testApp",[]).controller("testCtrl",function($scope){vardata="Hello";$scope.getData=function(){returndata;}$scope.setData=function(newData){data=newData;}});这是View:{{getData()}}我的问题是Angular如何知道何时触发View中的getData()方法。单击事件将更改数据。然而它是一个私有(private)变

javascript - 继承和 TypeScript 错误 : X is not a constructor function type

我有一个父类(superclass),我希望从中继承其他两个类。下面列出了这些类(class)。当我编译时,试图继承的两个类提示父类(superclass)(给出相同的错误):“[类文件路径(在本例中为A)]不是构造函数类型”A.tsexportclassA{//privatefields...constructor(username:string,password:string,firstName:string,lastName:string,accountType:string){//initialisation}}B.tsimportA=require('./A);exportc

javascript - 这是在 Javascript/node.js 中执行私有(private)函数的正确方法吗?

我在node.js中写的一个类如下:module.exports=exports=function(){returnnewClassA()};functionClassA(){this.myvariable=0;}我有一个我想私有(private)的函数。据我了解,如果该函数是在构造函数之外声明的,它本质上将是一个静态函数,无法引用this.myvariable。处理这个问题的正确方法是像这样在构造函数中声明函数://withinconstructorthis.myFunction=functionmyFunction(){console.log(this.myvariable)}或者

javascript - 如果没有 hack,就不能在私有(private) JavaScript 函数中访问“this”对象吗?

我在一个项目上工作了一段时间,试图找出我做错了什么,当我最终将“错误”缩小到以下代码无法按我预期工作的事实时:functionAlpha(){this.onion='onion';functionBeta(){alert(this.onion);}Beta();}alpha1=newAlpha();//Alerts'undefined'但是,如果我将代码更改为:functionAlpha(){varself=this;this.onion='onion';functionBeta(){alert(self.onion);}Beta();}alpha1=newAlpha();//Aler

javascript - 当 myarray 在一个框架中时,为什么 myarray instanceof Array 和 myarray.constructor === Array 都为 false?

所以下面的代码会发出两次错误警报:window.onload=function(){alert(window.myframe.myarrayinstanceofArray);alert(window.myframe.myarray.constructor===Array);}当页面中有一个名为“myframe”的iframe包含一个名为“myarray”的数组时。如果数组被移动到主页(而不是iframe),那么代码会像预期的那样发出两次true警报。有谁知道这是为什么吗? 最佳答案 functionisArray(o){return

javascript - 在 Javascript 中访问私有(private)成员的更好方法

在阅读了一些关于Javascript的prototypicalinheritancemodel之后,我从改变了构建类的风格varSome_Class=function(){this.public_method=function(){};(function(){//constructor}).call(this)}到varSome_Class=function(){(function(){//constructor}).call(this)}Some_Class.prototype.public_method=function(){};虽然我知道这是一个很好的做法,但我不能再从公共(pu

javascript - 谷歌地图 API v3 - TypeError : Result of expression 'google.maps.LatLng' [undefined] is not a constructor

我正在创建一个静态html页面来显示数据中的多个位置。我刚刚复制了其中一个示例并正在向后工作,但我在Safari检查器中收到以下错误:main.js:1SyntaxError:Parseerrorsample.htm:10TypeError:Resultofexpression'google.maps.LatLng'[undefined]isnotaconstructor.这是我的html代码:MultiMarkersSampleviaGoogleMapsfunctioninitialize(){varmyLatlng=newgoogle.maps.LatLng(-30.2965590

javascript - 如何从 JavaScript 模块模式中的私有(private)函数中调用公共(public)函数

如何从JavaScript模块模式中的私有(private)函数调用公共(public)函数?例如,在下面的代码中,varmyModule=(function(){varprivate1=function(){//Howtocallpublic1()here?//this.public1()won'twork}return{public1:function(){/*dosomething*/}}})();这个问题有人问过twicebefore,每个都有不同的可接受答案。在返回之前保存对返回对象的引用,然后使用该引用访问公共(public)方法。参见answer.在闭包中保存对公共(pu