Javascript 使用参数时 Typedef 错误
Javascript Typedef Error when using parameters
我做错了什么,如何在同一包装中将变量传递给不同的函数 variable/function。
示例:
function customFunctionWrap(){
this.myVar1 = 0;
this.getCurrentPosition = function(){
if (navigation.geolocation) {
navigator.geolocation.getCurrentPosition(function(position){});
}
},
this.doSomething = function(){ // Works
//Do something, return
this.callWithParams(); //Works
},
//If I remove passing in 'value1',calling it elsewhere works
this.doSomethingWithParams = function(value1){
//Use value1
//Return
},
this.callWithParams = function(){
var value1 = 'xyz'; //Is a variable that changes based on some DOM element values and is a dynamic DOM element
this.doSomethingWithParams(value1); //THROWS TYPEDEF ERROR: this.doSomethingWithParams is not a function
this.getCurrentPosition();
}
};
var local = new customFunctionWrap();
local.doSomething(); //WORKS
我知道还有另一种方法可以直接使用 customFunctionWrap.callWithParams(),但我想了解为什么前一种方法会出错。
var customFunctionWrap = {
myVar1 : 0,
callWithParams : function(){
}
}
JS 看到的内容:
var customFunctionWrap = (some function)()
returned 函数被触发,因为最后一个 (),所以它必须 yield/return 一些东西,否则,就像在你的代码中它是 "returning" undefined.
所以你给出的代码不起作用。
第一个修复是从
中删除最后 2 个字符
var customFunctionWrap = (some function)()
使其成为 return 构造函数。
我做错了什么,如何在同一包装中将变量传递给不同的函数 variable/function。
示例:
function customFunctionWrap(){
this.myVar1 = 0;
this.getCurrentPosition = function(){
if (navigation.geolocation) {
navigator.geolocation.getCurrentPosition(function(position){});
}
},
this.doSomething = function(){ // Works
//Do something, return
this.callWithParams(); //Works
},
//If I remove passing in 'value1',calling it elsewhere works
this.doSomethingWithParams = function(value1){
//Use value1
//Return
},
this.callWithParams = function(){
var value1 = 'xyz'; //Is a variable that changes based on some DOM element values and is a dynamic DOM element
this.doSomethingWithParams(value1); //THROWS TYPEDEF ERROR: this.doSomethingWithParams is not a function
this.getCurrentPosition();
}
};
var local = new customFunctionWrap();
local.doSomething(); //WORKS
我知道还有另一种方法可以直接使用 customFunctionWrap.callWithParams(),但我想了解为什么前一种方法会出错。
var customFunctionWrap = {
myVar1 : 0,
callWithParams : function(){
}
}
JS 看到的内容:
var customFunctionWrap = (some function)()
returned 函数被触发,因为最后一个 (),所以它必须 yield/return 一些东西,否则,就像在你的代码中它是 "returning" undefined.
所以你给出的代码不起作用。
第一个修复是从
中删除最后 2 个字符var customFunctionWrap = (some function)()
使其成为 return 构造函数。