how to use object inside another objects function
I have an object 'params' and another object 'options' whith function assigned 开发者_运维问答to its parameter. And I want to use 'params' element inside 'options' function. How can I do that?
var params = {
'errorId': 'dd.error',
} $(function() {
var options = {
'success': function() { alert(params['errorId']; }
});
Try removing var
keyword from params
to make it available later/globally:
params = {
'errorId': 'dd.error',
} $(function() {
var options = {
'success': function() { alert(params['errorId']; }
});
Hm. It looks like you're missing a curly brace after options. What doesn't work? Do you get a syntax error? The code there, other than the missing curly, looks like it should work just fine.
There's some errors in your posted code.
It should look more like this: Live example
var params = {
'errorId': 'dd.error',
};
$(function() {
var options = {
'success': function() {
alert(params['errorId']); // <--- added missing ')'
}
}; // <-- added missing '}' to options var
options.success(); // <--- added by me to execute function
});
精彩评论