简体   繁体   English

Javascript中类和对象之间的区别

[英]Difference between a class and an object in Javascript

What's the difference between 有什么区别

var myView = function () {
//something goes here
};

and

var myView = function () {
//something goes here
    return {
        a: x,
        b: y
}();

I think the first snippet creates a "dynamic" class, so that you can say 我认为第一个片段创建了一个“动态”类,所以你可以说

var anotherView = new myView();

and the second snippet is similar to a singleton "dynamic" object, but I'm not very sure. 第二个片段类似于单个“动态”对象,但我不太确定。

Javascript uses prototypal inheritance, so there are no classes per se. JavaScript使用原型继承,所以没有阶级本身。 Everything is an object; 一切都是一个对象; it's just that some objects have a common parent object whose methods/variables will be found when name resolution looks up the prototype chain. 只是某些对象有一个共同的父对象,当名称解析查找原型链时,会找到其方法/变量。

Your first code snippet creates an object called myView whose type is a function. 您的第一个代码段创建了一个名为myView的对象,其类型是一个函数。 Your second snippet defines an anonymous method which returns an object (with two properties, a and b ) and then immediately calls this method, assigning the result to myView . 你的第二个片段定义了一个匿名方法,它返回一个对象(有两个属性, ab ),然后立即调用这个方法,将结果分配给myView So in this second case, myView is an object with two self-defined properties. 所以在第二种情况下, myView是一个具有两个自定义属性的对象。

It may help you to read Douglas Crockford's description of prototypal inheritance in Javascript , as it sounds like you're a little fuzzy on the details. 它可能会帮助你阅读道格拉斯克罗克福德在Javascript中原型继承的描述,因为它听起来你对细节有点模糊。

There are no classes in javascript. javascript中没有类。

As you mentioned, your first example would be for a re-usable object, whereas your second example is just for a singleton object. 正如您所提到的,您的第一个示例是针对可重用的对象,而您的第二个示例仅针对单个对象。

The main difference here is that you're invoking that function immediately in the second example and it returns an object to you, whereas you need to explicitly invoke the first function each time using something like a=new myView() it's the () that's providing that invocation. 这里的主要区别是你在第二个例子中立即调用该函数并且它向你返回一个对象,而你需要每次使用类似a=new myView()东西显式调用第一个函数它是()提供该调用。

I use your 2nd example (known as crockford's module pattern) for one off page related tasks, and the first example for re-usable components within that page (some element generated many times with handlers etc) 我将第二个示例(称为crockford的模块模式)用于一个关闭页面的相关任务,以及该页面中可重用组件的第一个示例(使用处理程序等多次生成一些元素)

Also read about protoypal inheritance so you can understand how to effectively use the first example for writing better performing javascript code. 另请阅读有关原型继承的内容,以便您可以了解如何有效地使用第一个示例来编写性能更好的javascript代码。

var myView = function () { //something goes here };

This is function expression without being executed. 这是函数表达式而不执行。 And var myView = function () { //something goes here return { a: x, b: y }(); 并且var myView = function () { //something goes here return { a: x, b: y }(); This function expression gets executed due to parenthesis "()" place after function resulting in return of Object. 由于括号“()”在函数之后放置导致返回Object,因此执行此函数表达式。

Again New keyword use for creating constructor and can not be applicable for Object. New关键字用于创建构造函数,不适用于Object。

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

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