通过函数实例访问JS函数变量
Accessing JS function variables through function instance
我只是试图以不同的方式理解函数中定义的变量的行为。所以我只是尝试了一些代码,并在下面的评论中得出结论。如果我理解有什么不对的地方,请告诉我。或者,如果我错过了什么,就加一句。
我想知道如何通过函数实例(objTempFun1
和objTempFun2
)访问函数(如functionName.variableName
)上声明的变量,就像我可以在函数内部定义方法以通过函数实例公开闭包变量一样。
function tempFun() {
this.val1 = "this.var1";
var val2 = "var var2"; //locally scoped var, not added to function instance nor to the prototype
this.getLocalVar = function () {
return val2;
};
this.setLocalVar = function (arg) {
val2 = arg;
};
}
tempFun.prototype.val3 = "fun.proto.var3";
tempFun.val4 = "fun.var4";
var objTempFun1 = new tempFun();
var objTempFun2 = new tempFun();
/*-----------------------------------------------------------------------
Variables defined as `this.variableName`
1. Are declared on prototype
2. Have separate values across instances
3. Cannot be accessed on function itself
-----------------------------------------------------------------------*/
document.write("<br />obj1-this.var1: " + objTempFun1.val1 + "<br />");
document.write("obj2-this.var1: " + objTempFun2.val1 + "<br />");
document.write("fun-this.var1: " + tempFun.val1 + "<br />");
objTempFun1.val1 = "this.var1x";
objTempFun2.val1 = "this.var1y";
document.write("obj1-this.var1: " + objTempFun1.val1 + "<br />");
document.write("obj2-this.var1: " + objTempFun2.val1 + "<br />");
/*-----------------------------------------------------------------------
Variables defined inside function as `var variableName`
1. Are closure-scoped; defined neither on function nor on prototype
2. Have separate values across function instances
3. Can only be accessed through function instance by adding methods on
a prototype (as above getLocalVar and setLocalVar methods)
-----------------------------------------------------------------------*/
document.write("obj1-var var2: " + objTempFun1.val2 + "<br />");
document.write("obj2-var var2: " + objTempFun2.val2 + "<br />");
document.write("fun-var var2: " + tempFun.val2 + "<br />");
document.write("obj1-printLocalVar: " + objTempFun1.getLocalVar() + "<br />");
document.write("obj2-printLocalVar: " + objTempFun2.getLocalVar() + "<br />");
objTempFun1.setLocalVar("var var2x");
objTempFun2.setLocalVar("var var2y");
document.write("obj1-printLocalVar: " + objTempFun1.getLocalVar() + "<br />");
document.write("obj2-printLocalVar: " + objTempFun2.getLocalVar() + "<br />");
/*-----------------------------------------------------------------------
Variables defined as `functionName.prototype.variablename`
1. Are declared on prototype
2. Have separate values across function instances
3. Cannot be accessed on function itself
-----------------------------------------------------------------------*/
document.write("obj1-this.proto.var3: " + objTempFun1.val3 + "<br />");
document.write("obj2-this.proto.var3: " + objTempFun2.val3 + "<br />");
document.write("fun-this.proto.var3: " + tempFun.val3 + "<br />");
objTempFun1.val3 = "fun.proto.var3x";
objTempFun2.val3 = "fun.proto.var3y";
document.write("obj1-this.proto.var3: " + objTempFun1.val3 + "<br />");
document.write("obj2-this.proto.var3: " + objTempFun2.val3 + "<br />");
/*------------------------------------------------------------------------
Variables defined as `functionName.variablename`
1. Become member of function (or say 'F'unction instance, which is tempFun)
not the function instances (which are objTempFun1 & objTempFun2 above)
2. Cannot be accessed on function instances
------------------------------------------------------------------------*/
document.write("obj1-fun.var4: " + objTempFun1.val4 + "<br />");
document.write("obj2-fun.var4: " + objTempFun2.val4 + "<br />");
document.write("fun-fun.var4: " + tempFun.val4 + "<br />");
在此处查找JSFiddle。
constructor
属性提供对构造对象的函数的访问。
相关文章:
- 为什么我的控制器没有启动函数.js脚本
- 在函数js中传递变量
- 如何在单独的文件中调用函数 js.
- 在铯中调用函数.js当事件被尝试时
- 只有setTimeout执行函数.JS
- 为什么图像选择器程序中的这个函数 js 不起作用
- 获取邮件中的信息.php从函数.js解析为manage.html
- 如何从应用程序中调用JS函数.js在Rails4视图中
- Uncatch TypeError: obj.attr 不是函数 JS
- 从 C++ 节点调用 JavaScript 函数.js扩展
- 调用 Backbone 内部的外部函数.js的视图初始化方法
- 如何使用 Scala 创建 JavaScript 函数.js
- 使用带有下划线循环的回调函数.js骨干.js项目不起作用
- 获取字段值的函数-JS
- 函数js不能递归调用自己的函数
- 在函数JS中创建带有地理代码的标记
- 循环内部的函数(js/jquery)
- 我需要帮助将此代码添加到jQuery函数.js文件中
- 未捕获类型:未定义不是函数JS错误
- 停止多个内联函数JS的事件传播