简体   繁体   English

在JavaScript中的函数之间传递变量

[英]Passing variables between functions in Javascript

I have this code: 我有以下代码:

collection = (function() {

    function collection(removeLinkTitle){

        this.removeLinkTitle = removeLinkTitle || 'delete';
    }

    collection.prototype = {

        removeLinkTitle:        this.removeLinkTitle,

        init:function(){

            ...some code...
            this.deleteCollectionForm();
        },

        deleteCollectionForm:function(){

        var removeFormA = $('<a href="#">'+this.removeLinkTitle+'</a>');
        linkLi.append( removeFormA );

        removeFormA.on('click', function(e) {

            e.preventDefault();

            linkLi.remove();

            var index = collectionHolder.data( 'index' );
            collectionHolder.data( 'index', index - 1 );
        });
    }
};

return collection;

})();

The thing is that the var removeForm returns its value only the frst time it loads, the following times it returns undefined. 事实是,var removeForm仅在第一次加载时返回其值,而在随后的几次返回undefined时才返回。

I don't want to pass the variable as an argument so, is it there any other way to do this? 我不想将变量作为参数传递,是否还有其他方法可以实现?

Thanks !! 谢谢 !!

i think this is not really the problem, the one thing that is undefined is the, removeLinkTitle, try this: 我认为这不是真正的问题,未定义的一件事是removeLinkTitle,请尝试以下操作:

return new collection;

so you hit your constructor, or set some other value with: 因此,您可以使用构造函数,或使用以下方法设置其他值:

return new collection("delete item");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM