Javascript 对象创建最佳实践

发布于 2024-11-08 13:34:44 字数 1431 浏览 4 评论 0原文

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

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

发布评论

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

评论(4

暮光沉寂 2024-11-15 13:34:44

引用 Xhalent 的精彩文章(做得非常好,写得很清楚)由他:

这是因为“this”的值为
与“this”的值不同时
对象已创建。

所以在你的情况下:

...
  var _this = this.prop3;
  function helperFunc() {
    return _this;
  }
...

可能会达到预期的效果。

To cite Xhalent's wonderful article (really well done and clearly wirtten) mentioned by him:

That is because the value of “this” is
different to the value of “this” when
the object was created.

So in your case:

...
  var _this = this.prop3;
  function helperFunc() {
    return _this;
  }
...

might achieve what's desired.

苏别ゝ 2024-11-15 13:34:44

我已经完成了一系列关于基本 javascript 基础知识的文章 - 这里介绍了对象和原型:

Javascript 对象实例化和原型

我在这里深入研究闭包:Javascript 闭包

希望它们有所帮助。干杯。

I've done a series on basic javascript fundamentals - objects and prototypes are covered here:

Javascript Object Instantation and Prototypes

I delve into Closures here:Javascript Closures

Hope they help. Cheers.

孤寂小茶 2024-11-15 13:34:44

如果您有一个使用 this 的函数,则必须确保调用上下文正确。即使用 this.helperFunc(),而不仅仅是 helperFunc() (但您还需要进行一些设置,以便定义 this.helperFunc)。您的示例中 helperFunc 内的 this 的指称与 Create() 中的指称不同。

您可以将其视为函数不是作为对象的成员定义,而是作为对象的成员调用

根据上下文,this 可能会解析三件事。

  1. 如果函数调用前面有 new 关键字,则为新创建的对象。
  2. 调用函数时点左侧的对象。
  3. 全局对象(window)(如果以上两者均未提供)。

如果在没有对象的情况下调用函数,就像在 this.Create 中调用 helperFunc 的情况一样,this 将绑定到全局对象(window,当在浏览器中使用时)

给定这样的东西:

var o = {someVal:"hello"};
o.doSomething = function (){alert(this.someVal)};

调用o.doSomething()显然会警告“hello”。

给出:

var o2  = {someVal:"world"};
o2.someFunc = o.doSomething;

调用 o2.someFunc() 将警告“world”,而不是“hello”,正如您所期望的,如果它是指向 doSomething 成员的指针Ø 。

并给出:

var someFunc = o.doSomething
someVal = "sailor"

调用 someFunc() 将提醒“sailor”。

另一个令人困惑的地方是直接在 Setup() 中使用 this。当您使用 new 调用函数时,正如您所做的那样,this 不会绑定到全局对象,而是绑定到 Setup 的新实例> 对象。

对于上面的示例,这意味着调用 new o.doSomething() 将发出“未定义”警报,因为为调用创建的新对象没有“someVal”成员。

If you have a function that uses this, you must make sure that the calling context is correct. i.e. use this.helperFunc(), not just helperFunc() (but you'll also need to set things up so that this.helperFunc is defined). The referent of this inside helperFunc in your example is not the same as it is in Create().

You can think of it as though functions are not defined as members of objects, but called as members of objects.

There are three things that this might resolve to, depending on context.

  1. A newly created object, if the function call was preceded by the new keyword.
  2. The Object to the left of the dot when the function was called.
  3. The Global Object (window), if neither of the above are provided.

If a function is called without an object, as in the case of your call to helperFunc in this.Create, this will be bound to the global object (window, when used in a browser)

Given something like this:

var o = {someVal:"hello"};
o.doSomething = function (){alert(this.someVal)};

Calling o.doSomething() will obviously alert "hello".

Given:

var o2  = {someVal:"world"};
o2.someFunc = o.doSomething;

Calling o2.someFunc() will alert "world", not "hello", as you would expect if it were a pointer to the doSomething member of o.

And given:

var someFunc = o.doSomething
someVal = "sailor"

Calling someFunc() will alert "sailor".

Another point of confusion is the use of this directly within Setup(). When you call a function using new, as you have done, this is not bound to the global object, but to a new instance of the Setup object.

For my example above, this means that calling new o.doSomething() will alert "undefined", as the new object that has been created for the call does not have a "someVal" member.

掩饰不了的爱 2024-11-15 13:34:44

此页面上有 Mike Koss 撰写的关于 JS 中的 OOP 的精彩教程。

我见过它在 jQuery 中被大量使用
插件,但第一个是什么
括号后跟空括号
最后的意思是什么?

第二组括号立即调用您在第一组括号中声明的函数。

您可以声明它并单独执行它(允许执行多次):

var myFunction = function() { alert("boo"); }; // Declare & instanciate
myFunction(); // Invoke
myFunction(); // Invoke again

或者在一行中执行两者:

(function() { alert("boo"); })(); // Can only invoke now, the function is anonymous and not stored anywhere.

You have a good tutorial by Mike Koss on OOP in JS on this page.

I've seen it used a lot in jQuery
plugins but what does the first
parenth followed by empty parenth at
the end mean and do?

The second set of parenth immediatly invokes the function you have declared in the first set of parenth.

You could declare it and execute it separatly (allows to execute multiple times):

var myFunction = function() { alert("boo"); }; // Declare & instanciate
myFunction(); // Invoke
myFunction(); // Invoke again

Or do both in one line:

(function() { alert("boo"); })(); // Can only invoke now, the function is anonymous and not stored anywhere.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文