简体   繁体   English

从JavaScript对象内部的函数中引用函数

[英]Referencing functions from within functions inside a JavaScript object

var Page = {
    data: null,
    Load: function () {
        this.Populate;
    },
    Populate: function () {
    }
};

$(document).ready(Page.Load);
  1. Why can't I reference Page.Load as a function in ready() eg .ready(Page.Load()) 为什么我不能在ready()中将Page.Load作为函数引用,例如.ready(Page.Load())
  2. Why can't I call this.Populate() from the Load function, I just get this.Populate() is not a function. 为什么我不能从Load函数调用this.Populate() ,我只是得到了this.Populate()不是函数。

Why can't I reference Page.Load as a function in ready() eg .ready(Page.Load()) 为什么我不能在ready()中将Page.Load作为函数引用,例如.ready(Page.Load())

Sticking () on the end calls a function. 最后贴上() 调用一个函数。 You want to pass the function itself, not its return value. 您要传递函数本身,而不是其返回值。 (The ready function will call what you pass to it later). ready函数将在以后调用传递给它的内容)。

Why can't I call this.Populate() from the Load function, I just get this.Populate() is not a function 为什么我不能从Load函数调用this.Populate(),我才得到this.Populate()不是函数

Two reasons. 有两个原因。

First, you never actually call the Populate function. 首先,您实际上从未调用过Populate函数。 Change to: 改成:

this.Populate()

Second: because you detach the Load function from the object, so this isn't Page by the time it gets called. 第二:因为您分离从对象负载的功能,所以this不是Page通过它被调用的时间。

Do this instead: 改为这样做:

$(document).ready(function () { Page.Load() });

A function call should be in the format function_name() (parentheses!). 函数调用的格式应为function_name() (括号!)。 A function reference should be function_name (without parentheses). 函数引用应为function_name (不带括号)。 This code will work: 该代码将起作用:

var Page = {
    data: null,
    Load: function () {
        Page.Populate(); //PARENTHESES
    },
    Populate: function () {
    }
};

$(document).ready(Page.Load);//NO PARENTHESES

$(document).ready is a function which expects a function to be parameter 1. If you use Page.Load() , you will execute function Page.Load() and pass the return value ( undefined , in this case) to $(document).ready . $(document).ready是一个期望该函数为参数1的函数。如果使用Page.Load() ,则将执行函数Page.Load()并将返回值(本例中为undefined )传递给$(document).ready

There are two problems in your code. 您的代码中有两个问题。

First, as Rob says, you're not actually calling the Populate() method in Load() . 首先,正如Rob所说,实际上并没有在Load()调用Populate()方法。 You need to add parenthesis to do that. 您需要添加括号才能做到这一点。

Second, the this keyword in Populate() does not refer to the Page object. 其次, Populate()中的this关键字不引用Page对象。 You can use $.proxy() to set the Page object as the context of the method call: 您可以使用$ .proxy()Page对象设置为方法调用的上下文:

var Page = {
    data: null,
    Load: function() {
        this.Populate();
    },
    Populate: function() {
    }
};

$(document).ready($.proxy(Page.Load, Page));

You can get it to work by wrapping your object up in a function like this: 您可以通过将对象包装在如下函数中来使其工作:

var Page = (function () {
    var that = {
        data: null,
        Load: function () {
            that.Populate(); 
        },
        Populate: function () {
            alert("Test");
        }
    };
    return that;
})(); // Execute the function immediately to return 'that'

$(document).ready(Page.Load);

This way you can call between methods as you have a reference to the object (that). 这样,您可以在对对象的引用时在方法之间进行调用。

JSBin Example JSBin示例

You're calling the function Page.Load. 您正在调用函数Page.Load。 But more specifically you're calling the function that Page.Load is set to (since that argument to .ready() is just a simple value); 但更具体地说,您要调用Page.Load设置为的函数(因为.ready()的参数只是一个简单值); you're not calling it on Page, so 'this' is not reset to point to the Page object. 没有 Page 调用它,因此不会重置“ this”以指向Page对象。

This will work better: 这将更好地工作:

$(document).ready(function() {
    Page.Load();
});

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

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