构造函数和Object之间的区别 [英] Difference between a constructor and an Object

查看:115
本文介绍了构造函数和Object之间的区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



有什么不同:

  var MY_APP = function(){
this.firstMethod = function(){
// something
};
this.secondMethod = function(){
// something
};
};

  var MY_APP = {
firstKey:function(){
// something
},
secondKey:function(){
// something
}
};

除了一个明显的事实,即一个是Function而另一个是Object ,代码流,原型,模式有什么区别...什么,什么时候我们应该使用第一个或第二个?



我很间隔这个领域,我不知道我是否正确地解释了疑问,但如果你问,可以给出进一步的信息。

解决方案

两者之间的主要区别在于它们打算如何使用。正如其名称所暗示的,构造函数旨在创建和设置对象的多个实例。另一方面,对象文字是一次性,就像字符串和数字文字一样,并且更经常用作配置对象或全局单例(例如,用于命名空间)。



第一个例子有一些细节需要注意:


  1. 执行代码时,匿名函数是创建并分配给MY_APP,但没有其他事情发生。 firstMethod secondMethod 在明确调用MY_APP之前不存在

  2. 根据调用MY_APP的方式,方法 firstMethod secondMethod 将会在不同的地方出现:


    1. MY_APP():由于没有提供上下文,所以 this 默认为 window ,并且这些方法将变为全局的。
    2. var app1 = new MY_APP():由于 new 关键字,创建一个新对象并成为默认上下文。 这个引用新对象,并且这些方法将被分配给新对象,随后被分配给 app1 。但是, MY_APP.firstMethod 仍未定义。
    3. MY_APP.call(YOUR_APP):这会调用我的MY_APP,但将上下文设置为另一个对象, YOUR_APP 。这些方法将分配给 YOUR_APP ,覆盖具有相同名称的 YOUR_APP 的任何属性。这是一个非常灵活的方法,它允许在Javascript中使用多重继承或混入。


构造函数还允许另一层次的灵活性,因为函数提供了闭包,而对象文本没有。如果例如 firstMethod secondMethod 依靠一个公共变量 password 对象是私有的(不能在构造函数之外访问),这可以通过以下操作实现:

  var MY_APP = function(){
var password =GFHSFG;

this.firstMethod = function(){
//用密码
做某事alert(password); // Woops!
};
this.secondMethod = function(){
//用密码
做其他事情;
};

MY_APP();

alert(密码); //未定义
alert(MY_APP.password); //未定义


I definitely need some light on this.

What's the diference between:

var MY_APP = function(){
    this.firstMethod = function(){
       //something
    };
    this.secondMethod = function(){
       //something
    };
};

and

var MY_APP = {
    firstKey: function(){
       //something
    },
    secondKey: function(){
       //something
    }
};

besides the obvious fact that one is a Function and the other an Object, what are the differences in code flow, prototypes, patterns... whatever, and when should we use the first or the second?

I'm so spaced out in this area that i'm not sure if i'm correctly explaining the doubt, but further info can be given if you ask.

解决方案

The key difference between the two is in how they are intended to be used. A constructor, as its name suggests, is designed to create and set up multiple instances of an object. An object literal on the other hand is one-off, like string and number literals, and used more often as configuration objects or global singletons (e.g. for namespacing).

There are a few subtleties about the first example to note:

  1. When the code is executed, an anonymous function is created and assigned to MY_APP, but nothing else happens. firstMethod and secondMethod don't exist until MY_APP is explicitly called.
  2. Depending on how MY_APP is called, the methods firstMethod and secondMethod will end up in different places:

    1. MY_APP(): Since no context is supplied, the this defaults to window and the methods will become global.
    2. var app1 = new MY_APP(): Due to the new keyword, a new object is created and becomes the default context. this refers to the new object, and the methods will get assigned to the new object, which subsequently gets assigned to app1. However, MY_APP.firstMethod remains undefined.
    3. MY_APP.call(YOUR_APP): This calls my MY_APP but sets the context to be another object, YOUR_APP. The methods will get assigned to YOUR_APP, overriding any properties of YOUR_APP with the same names. This is a really flexible method that allows multiple inheritance or mixins in Javascript.

Constructors also allow another level of flexibility since functions provide closures, while object literals do not. If for example firstMethod and secondMethod rely on a common variable password that is private to the object (cannot be accessed outside the constructor), this can be achieved very simply by doing:

var MY_APP = function(){
    var password = "GFHSFG";

    this.firstMethod = function(){
       // Do something with password
       alert(password); // Woops!
    };
    this.secondMethod = function(){
       // Do something else with password
    };
};

MY_APP();

alert(password); // undefined
alert(MY_APP.password); // undefined

这篇关于构造函数和Object之间的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆