变量undefined在自调用函数中

时间:2017-07-04 02:19:13

标签: javascript jquery

我想知道为什么我的未定义如果我这样声明:

var mainBtn = $('.main-button'),
    cloneFund = (function(mainBtn) {
        console.log(mainBtn); // undefined
        return arguments.callee;
    })();

甚至:

var btn = $('.main-button'),
    cloneFund = (function(btn) {
        console.log(btn); // undefined
        return arguments.callee;
    })();

2 个答案:

答案 0 :(得分:1)

你必须将mainBtn传递给函数。

describe('Protractor Demo App', function() {
        browser.get('http://example.com');//url
        it("Test..", function() {
        element(by.model('customer.firstName')).sendKeys('FIRSTNAME');
        element(by.model('customer.lastName')).sendKeys('LASTNAME');
       });
    });
var mainBtn = $('.main-button'),
    cloneFund = (function(mainBtn) {
        console.log(mainBtn); // available
        return arguments.callee;
    })(mainBtn); // here you have to pass

答案 1 :(得分:1)

你应该:

var btn = $('.main-button'),
    cloneFund = (function(btn) {
        console.log(btn); // undefined
        return arguments.callee;
})(btn); // need to add the argument when invoked