admin管理员组文章数量:1418590
I have an object parameter
and it has a property value
. it is thusly defined
var parameter = {
value : function() {
//do stuff
}
};
my problem is that, in some cases, value needs to have a property of its own named length
can i do that? it seems that putting this.length = foo
does not work, neither does parameter.value.length = foo
after the object declaration.
I have an object parameter
and it has a property value
. it is thusly defined
var parameter = {
value : function() {
//do stuff
}
};
my problem is that, in some cases, value needs to have a property of its own named length
can i do that? it seems that putting this.length = foo
does not work, neither does parameter.value.length = foo
after the object declaration.
-
Do you mean that the returned object from
value()
needs to have a length? – Hemlock Commented Jan 25, 2011 at 17:22 -
no, i check
value
's length and iterate over it as though it were an array. – griotspeak Commented Jan 25, 2011 at 17:37
3 Answers
Reset to default 6The problem seems to be with the selection of the word 'length'. In JavaScript, functions are objects and can have properties. All functions already have a length property which returns the number of parameters the function is declared with. This code works:
var parameter = {
value : function() {
//do stuff
}
};
parameter.value.otherLength = 3;
alert(parameter.value.otherLength);
parameter.value.length
should work. Run the following:
var obj = {
method: function () {}
};
obj.method.foo = 'hello world';
alert(obj.method.foo); // alerts "hellow world"
Functions are technically objects, so they can have methods and properties of their own.
Try this. It should work:
var parameter = {
value : {
length : ''
}
}
var newLength = parameter.value.length = 10;
alert( newLength ); // output: 10
If I understand your question, basically, in object literals notation, an object can contain another object and so on. So to access the inner object and its properties, you just have to do the dot natation as usual following the hierarchy. In the above case 'parameter' then the inner-object, 'value', then the inner-object property, 'length'.
本文标签: propertiesFunction with a property in javascriptStack Overflow
版权声明:本文标题:properties - Function with a property in javascript? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745297170a2652149.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论