admin管理员组文章数量:1220832
var obj = {
func: function() {
return: {
add: function() {
}
}
},
somefunc: function() {
}
}
The orginal code behind where i used to convert this...
var hash = (function() {
var keys = {};
return {
contains: function(key) {
return keys[key] === true;
},
add: function(key) {
if (keys[key] !== true){
keys[key] = true;
}
};
})();
Questions:
- What is the use of return keyword?
- Can i structure like this, my class?
var obj = {
func: function() {
return: {
add: function() {
}
}
},
somefunc: function() {
}
}
The orginal code behind where i used to convert this...
var hash = (function() {
var keys = {};
return {
contains: function(key) {
return keys[key] === true;
},
add: function(key) {
if (keys[key] !== true){
keys[key] = true;
}
};
})();
Questions:
- What is the use of return keyword?
- Can i structure like this, my class?
3 Answers
Reset to default 16On the most basic level, the return
keyword defines what a function should return. So if I have this function:
function foo() { return 4; }
And then call it:
var bar = foo();
foo()
will return 4
, hence now the value of bar
is also 4
.
Onto your specific example:
In this use case the return
is used to basically limit outside access to variables inside the hash
variable.
Any function written like so:
(function() {...})();
Is self-invoking, which means it runs immediately. By setting the value of hash
to a self-invoking function, it means that code gets run as soon as it can.
That function then returns the following:
return {
contains: function(key) {
return keys[key] === true;
},
add: function(key) {
if (keys[key] !== true){
keys[key] = true;
}
}
};
This means we have access to both the contains
and add
function like so:
hash.contains(key);
hash.add(key);
Inside hash
, there is also a variable keys
but this is not returned by the self-invoking function that hash
is set to, hence we cannot access key
outside of hash
, so this wouldn't work:
hash.keys //would give undefined
It's essentially a way of structuring code that can be used to create private variables through the use of JavaScript closures. Have a look at this post for more information: http://net.tutsplus.com/tutorials/javascript-ajax/quick-tip-private-variables-in-javascript/
Hope this Helps :)
Jack.
An anonymous function that is executed immediately is commonly used to create a scope. Any variables that you declare inside the scope is local to the function, so they don't pollute the global scope.
The return statement is used to return an object from the anonymous function:
var hash = (function() {
return { ... };
})();
You could write the same using a named function:
function createHashObject() {
return { ... };
}
var hash = createHashObject();
I don't know what is your actual code is but these are the closures john
function addGenerator( num ) {
return function( toAdd ) {
return num + toAdd
};
}
var addFive = addGenerator( 5 );
alert( addFive( 4 ) == 9 );
in this you can see use of return
本文标签: javascriptWhat is the use of return statement inside a functionStack Overflow
版权声明:本文标题:javascript - What is the use of return statement inside a function? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1739265552a2155576.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
}
in the original code. – Šime Vidas Commented Jul 3, 2011 at 11:58