简体   繁体   English

Javascript内联函数调用

[英]Javascript inline function call

I have a question about inline function call in javascript. 我有一个关于javascript内联函数调用的问题。

This example works: 这个例子有效:

<button onclick="{alert($(this).text());}">Testing</button>

While this is not working: 虽然这不起作用:

<button onclick="function(){alert($(this).text());}">Testing</button>

My question is - why is the second case not working and the first one does? 我的问题是 - 为什么第二种情况不起作用而第一种情况呢?

I've come accross this issue, using jQuery-UI droppable: 我使用jQuery-UI droppable来解决这个问题:

$( ".selector" ).droppable({
  drop: function( event, ui ) {}
});

Droppable is also using this syntax (without function() ). Droppable也使用这种语法(没有function() )。 Why is that? 这是为什么?

<button onclick="function(){alert($(this).text());}">Testing</button>

That is equivalent to: 这相当于:

yourButton.addEventListener("click", function(){
    function(){
        alert($(this).text());
    };
});

Can you see why it's not working now? 你能明白为什么现在不行吗?

example 1 例1

Your passing statement. 你的传递声明。 So it's working. 所以它正在发挥作用。

example 2 例2

Your passing callback function. 你的传递回调函数。 Javascript onclick doesn't support callback. Javascript onclick不支持回调。

About droppable, droppable is a jquery plugin so it will support callback function. 关于droppable,droppable是一个jquery插件,所以它将支持回调函数。

Because the brackets are superfluous, and likely being ignored: 因为括号是多余的,可能会被忽略:

 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <button onclick="alert($(this).text());">Testing</button> 

If you want to encapsulate it in a function and run it as such, I suppose it would need to be self-invoking: 如果你想将它封装在一个函数中并像这样运行它,我想它需要自我调用:

 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <button onclick="(function(){alert($(this).text());})();">Testing</button> 

But then the this value gets unbound. 但是this值得到了解除。

The droppable syntax is something completely different-- you can think of it like this: droppable语法完全不同 - 您可以这样想:

var configObj = { // this isn't a function, it's a config object
  drop: function( event, ui ) {}  // this is a function value of the property drop
}
$( ".selector" ).droppable(configObj);

Lets break this down sample by sample 让我们按样品分解这个样品

<button onclick="{alert($(this).text());}">Testing</button>

This would be same as doing the following in pure javascript. 这与在纯JavaScript中执行以下操作相同。

document.querySelector('button').addEventListener('click', function() {
  {
    alert($(this).text());
  }
});

It's a bit weird to add extra braces like that but it is fully allowed and the code within will be executed. 添加类似的额外大括号有点奇怪,但它是完全允许的,并且将执行其中的代码。 For your second sample this gets really weird though. 对于你的第二个样本,这变得非常奇怪。

<button onclick="function(){alert($(this).text());}">Testing</button>

This would be the same as this. 这与此相同。

document.querySelector('button').addEventListener('click', function() {
  function() {
    alert($(this).text());
  }
});

In other words, when you click on the button you'll declare a new function but you'll never call it. 换句话说,当您单击按钮时,您将声明一个新功能,但您永远不会调用它。 To get around this you would need to wrap it in paranthesis and then call .call(this) on the function to execute with the same this as the caller. 为了解决这个问题,你需要将其包装在paranthesis,然后调用.call(this)在功能上具有相同执行this作为主叫方。

document.querySelector('button').addEventListener('click', function() {
  (function() {
    alert($(this).text());
  }).call(this);
});

Or in your style. 或者你的风格。

<button onclick="(function(){alert($(this).text());}).call(this)">Testing</button>

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

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