简体   繁体   English

将参数传递给javascript构造函数

[英]pass param to javascript constructor function

    function check_and_kill(selector){
        (function checkState(selector){
            if(selector.attr('type')){ 
                return false;
            }else{
                return true;
            }
        })(),

        kill = function(){
            if(this.checkState() == false){
                console.log('kill all')
            }
        }

    }
$('something').click(){
    check_and_kill($(this));
}

I try to make checkState as my constructor, the the second function which is kill react depends on the value returned by the constructor, but I can't pass the selector, it return undefined. 我尝试使checkState作为我的构造函数,第二个kill函数依赖于构造函数返回的值,但是我无法通过选择器,它返回的是undefined。

Following is the simulation of both cases. 以下是这两种情况的模拟。

Code

 function Class(str){ (function(str){ document.write(str) })(str) // Binding value } function Class2(str){ (function(str){ document.write(str) })() // Not binding value } (function(){ var a = new Class("Hello World. "); var b = new Class2(" Hello World ") })() 

You don't store the value from the constructor... 您不存储构造函数的值...

checkState = (function checkState(selector){
        if(selector.attr('type')){ 
            return false;
        }else{
            return true;
        }
})(),

or call the kill function... also you should take a long hard look at the syntax for your call to .click.... also .click doesn't have reference to a "this" 或调用kill函数...此外,您还应仔细查看调用.click ....的语法。此外.click没有引用“ this”

Here's a working example in the spirit of what you're trying to do: 以下是您正在尝试做的工作示例:

https://jsfiddle.net/n2zk1wym/ https://jsfiddle.net/n2zk1wym/

Here's a working example that does what you're trying to do, without all the bells and whistles. 这是一个可行的示例,可以完成您想做的事情,而没有所有麻烦。 Your attempt to use the finer aspects of the javascript language are commendable, but totally unnecessary for this particular problem: 您尝试使用javascript语言的更佳方面的尝试值得称赞,但对于此特定问题完全没有必要:

https://jsfiddle.net/2mL9jh2a/ https://jsfiddle.net/2mL9jh2a/

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

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