JavaScript 对象问题

发布于 2024-11-05 20:47:31 字数 309 浏览 4 评论 0原文

我是 Javascript 的初学者。我正在查看其他人编写的以下代码:

function MeetingPage()
{
   MeetingPage.colors = new Object();
}

...

var meeting = new MeetingPage();

根据我所看到的,我相信 MeetingPage 函数创建了一个对象,稍后有人在会议中保留该对象。什么是 MeetingPage.colors? MeetingPage 前缀是某种全局前缀吗?它是某种“this”指针吗?

任何建议将不胜感激。

I'm a beginner w/ Javascript. I'm looking at the following code that someone else wrote:

function MeetingPage()
{
   MeetingPage.colors = new Object();
}

...

var meeting = new MeetingPage();

From what I've seen, I believe that the MeetingPage function creates an object that later someone holds onto in meeting. What is the MeetingPage.colors? Is the MeetingPage prefix some kind of global? Is it a "this" pointer of some kind?

Any suggestions would be appreciated.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

蓝海似她心 2024-11-12 20:47:31

这实际上只是糟糕的代码。 MeetingPage.colors = new Object();MeetingPage function 上设置一个名为 colors 的属性,即:

function MeetingPage(){ }
MeetingPage.colors = {};

这是完全有效的,因为 JavaScript 中的所有函数都是对象。问题是,如果您有多个会议页面实例:

var meeting1 = new MeetingPage();
var meeting2 = new MeetingPage();

您发布的代码将重置颜色。它应该写为 this.colors = {},或者应该设置在函数的外部,如我的第一个代码段中所示。

It's actually just bad code. MeetingPage.colors = new Object(); is setting a property called colors on the MeetingPage function, i.e:

function MeetingPage(){ }
MeetingPage.colors = {};

Which is perfectly valid since all functions in JavaScript are objects. The problem is that if you have multiple instances of meeting page:

var meeting1 = new MeetingPage();
var meeting2 = new MeetingPage();

The code you posted will reset colors. It should either should be written as this.colors = {}, or it should be set outside of the function as in my first snippet.

美人如玉 2024-11-12 20:47:31

当我了解 javascript 中的不同对象模式时,这次演讲确实很有帮助。示例代码包含在第二个链接中(视频介绍了它)。

http://alexsexton.com/?p=94

http://alexsexton.com/inheritance/demo/

http://alexsexton.com/?p=51

当然,你也应该阅读 http://www.amazon.com/JavaScript-Good-Parts-Douglas-Crockford/dp/0596517742

HTH

This talk was really helpful when I got into different object patterns in javascript. Example code is included in the second link (the video introduces it).

http://alexsexton.com/?p=94

http://alexsexton.com/inheritance/demo/

http://alexsexton.com/?p=51

Of course, you should also definitely read http://www.amazon.com/JavaScript-Good-Parts-Douglas-Crockford/dp/0596517742

HTH

七分※倦醒 2024-11-12 20:47:31

这是创建类属性的 JavaScript 语法。请注意,它是一个属性,而不是实例属性,这意味着它在该类的所有实例之间共享。 (如果您了解 C++,这就像类静态)但是,我认为将类属性放入构造函数本身是无效的。我认为每次创建新的 MeetingPage 时,颜色类属性都会被清除。

This is the JavaScript syntax for creating Class Properties. Note, it is a class property not an instance property, that means it is shared across all instances of the class. (If you know C++ this is like a class static) However, I didn't think it was valid to put a class property inside the constructor itself. I would think that every time a new MeetingPage is created the colors class property will get wiped out.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文