访问返回另一个函数的嵌套函数中的属性
Accessing properties in nested function returning another function
有没有办法像这样访问嵌套函数中的属性:
function func(){
this.new_func=function(){
console.log("something");
return 'something';
}
this.someValue=7;
return function(){
return "something_diff";
};
}
var obj=new func();
obj(); //works with returning "something diff"
obj.new_func(); // TypeError: obj.new_func is not a function
obj.someValue; // undefined
我需要删除整个 "return function()..." 部分才能访问 "someValue" 和 "new_func()"。 为什么它会这样,有没有办法以某种方式访问这些属性,同时仍然返回另一个函数??
你可以这样做:
var parentFunction = function() {
var nestedFunction = function() {
var value = "nestedValue";
var moreValues = "more values";
return {
value: value,
moreValues: moreValues
}
}
var anotherNestedFunction = function() {
var anotherValue = "nestedValue";
return anotherValue;
}
return {
nested: nestedFunction,
another: anotherNestedFunction
}
}
然后:
var newFunction = new parentFunction();
var nested = newFunction.nested();
console.log("nested value: ", nested.value);
console.log("another nested value: ", newFunction.another);
Here 是一个工作示例:
Why is it acting like that, and is there a way to somehow access that properties, while still returning another function ??
因为 pharenteis:
var obj=new func();
基本上你正在触发你的函数,变量 obj 存储的是 "func" returns.
为了访问私有属性,你应该看看闭包:https://developer.mozilla.org/en-US/docs/Web/JavaScript/Closures
当您有一个 returns 对象的构造函数时,该对象将替换您分配给 this
的任何内容。所以确实,成员 new_func
和 someValue
丢失了。
要将返回的函数与其他成员组合在一起,您可以这样做:
function func() {
var f = function() {
return "something_diff";
};
f.new_func = function() {
console.log("something");
return 'something';
}
f.someValue = 7;
return f;
}
var obj = new func();
console.log(obj());
obj.new_func();
console.log('someValue:', obj.someValue);
有没有办法像这样访问嵌套函数中的属性:
function func(){
this.new_func=function(){
console.log("something");
return 'something';
}
this.someValue=7;
return function(){
return "something_diff";
};
}
var obj=new func();
obj(); //works with returning "something diff"
obj.new_func(); // TypeError: obj.new_func is not a function
obj.someValue; // undefined
我需要删除整个 "return function()..." 部分才能访问 "someValue" 和 "new_func()"。 为什么它会这样,有没有办法以某种方式访问这些属性,同时仍然返回另一个函数??
你可以这样做:
var parentFunction = function() {
var nestedFunction = function() {
var value = "nestedValue";
var moreValues = "more values";
return {
value: value,
moreValues: moreValues
}
}
var anotherNestedFunction = function() {
var anotherValue = "nestedValue";
return anotherValue;
}
return {
nested: nestedFunction,
another: anotherNestedFunction
}
}
然后:
var newFunction = new parentFunction();
var nested = newFunction.nested();
console.log("nested value: ", nested.value);
console.log("another nested value: ", newFunction.another);
Here 是一个工作示例:
Why is it acting like that, and is there a way to somehow access that properties, while still returning another function ??
因为 pharenteis:
var obj=new func();
基本上你正在触发你的函数,变量 obj 存储的是 "func" returns.
为了访问私有属性,你应该看看闭包:https://developer.mozilla.org/en-US/docs/Web/JavaScript/Closures
当您有一个 returns 对象的构造函数时,该对象将替换您分配给 this
的任何内容。所以确实,成员 new_func
和 someValue
丢失了。
要将返回的函数与其他成员组合在一起,您可以这样做:
function func() {
var f = function() {
return "something_diff";
};
f.new_func = function() {
console.log("something");
return 'something';
}
f.someValue = 7;
return f;
}
var obj = new func();
console.log(obj());
obj.new_func();
console.log('someValue:', obj.someValue);