gpt4 book ai didi

javascript - 从 JavaScript 对象内的函数中引用函数

转载 作者:行者123 更新时间:2023-11-30 07:03:07 24 4
gpt4 key购买 nike

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

$(document).ready(Page.Load);
  1. 为什么我不能将 Page.Load 引用为 ready() 中的函数,例如 .ready(Page.Load())
  2. 为什么我不能从 Load 函数中调用 this.Populate(),我只是得到 this.Populate() is not a function。

最佳答案

Why can't I reference Page.Load as a function in ready() eg .ready(Page.Load())

在末尾粘贴 () 调用一个函数。你想传递函数本身,而不是它的返回值。 (ready 函数将调用您稍后传递给它的内容)。

Why can't I call this.Populate() from the Load function, I just get this.Populate() is not a function

两个原因。

首先,您从未实际调用 Populate 函数。更改为:

this.Populate()

第二:因为您从对象中分离了 Load 函数,所以 this 在它被调用时不是 Page

改为这样做:

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

关于javascript - 从 JavaScript 对象内的函数中引用函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7567674/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com