javascript - 带有原型(prototype)函数名称.argument 的 JavaScript 函数调用如何工作?

标签 javascript

在下面的程序中,带有原型(prototype)函数名.argument 的 JavaScript 函数调用是如何工作的?

 function getLAdd() {
     // this sets all the variables containing positions of ball and bar with their respective ids.
     var ladd = 0;
     var pball = $("#ball");
     var pbar = $("#bar");
     var bar_position = pbar.position();
     var ball_position = pball.position();
     if (ball_position.top >= window.innerHeight - 100) {
         if (ball_position.left - 10 >= bar_position.left && ball_position.left - 10 <= bar_position.left + 100) {
             ladd = -2;
         }
         if (ball_position.left + 10 <= bar_position.left + 200 && ball_position.left + 10 >= bar_position.left + 100) {
             ladd = 2;
         }
     }
// how does getLAdd.ladd work ? Is this a type of dynamic call ?
     if (ladd == 0) {
         ladd = getLAdd.ladd;
     }
     if (ball_position.left <= 15 || ball_position.left >= window.innerWidth - 40) 
         ladd = -ladd;

     getLAdd.ladd = ladd;
     return ladd;
 }

最佳答案

JavaScript 中的函数是对象,因此您可以向它们添加属性。

在这段代码中,名为 ladd 的属性已添加到 getLAdd 函数中,并在此行中检索:

ladd = getLAdd.ladd;

并且正在这条线上更新:

getLAdd.ladd = ladd;

你可以用任何函数做同样的事情。

function f() {
       // get the property
    console.log(f.foo); // bar
}

   // add a property to the function object
f.foo = "bar";

   // get the property
console.log(f.foo); // bar

   // call the function
f();

关于javascript - 带有原型(prototype)函数名称.argument 的 JavaScript 函数调用如何工作?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13518392/

相关文章:

javascript - 有人可以告诉我在哪里可以很好地了解 Javascript 中的 Dot Notation

javascript - 安全地将 JSON 字符串转换为对象

javascript - 使用 Meteor 检测 3G 网络连接

javascript - 如何逐个记录多个时间值

javascript - 如何将带有双引号反斜杠的 JSON 字符串转换为 Javascript 对象

javascript - 使用 : Need Javascript code that gets the inner width/height of screen onload into width/height style variables

javascript - 如何让div中的文本相对于div的宽度和高度,CSS,HTML

javascript - 如何获取范围,然后在 Google Apps 脚本中设置值

javascript - Jquery:1个用于选择文件和文件上传的按钮?

javascript - 与父窗口一起滚动 iframe