对象字面量 vs 构造函数+原型 [英] Object literal vs constructor+prototype

查看:32
本文介绍了对象字面量 vs 构造函数+原型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对象字面量=用大括号括起来的名称值对.

Object literal=name value pairs wrapped in curly braces.

构造函数=用于使用关键字 new 创建多个实例的函数.

Constructor=a function used to create multiple instance using keyword new.

原型=用于文字的扩展.

这是我迄今为止所理解的.但是我研究得越多,我就越对它们每个的重要性感到困惑.我在我的代码中使用了几次构造函数、原型和文字.但是每次使用它们时,我觉得我仍然没有意识到它的全部潜力.我现在想在成为初学者之前先走一步.我希望 stackoverflow 的人们帮助我实现它

This is what I have understood till now.But the more I research,More I get confused about what is the significance of each one of them. I have used constructor,protoypes and literals in my code a few times.But everytime I use them,I feel like I am still not aware of the full potential of it.I want to go one step ahead of being a begineer now.I hope Folks at stackoverflow help me achieve it

  1. 哪种是最佳首选编程方式(对象文字 vs 构造函数 vs 原型)

可以只使用对象字面量编写带有构造函数和原型的代码吗没有使用构造函数和原型.

can a code with constructor and protoype be written using just object literals without using constructor and protoype.

匿名函数的意义是什么.

一个非常简单的例子也可以证明它们的重要性.我知道它们是什么,但我不知道它们可以做什么.

A very simple example demonstrating their importance will also do.I am aware of what they are but I am not aware of what possible magic they can do.

推荐答案

对象字面量和函数之间有一个(在我看来是根本的)区别,即私有"区别.变量.由于一个对象不能被实例化(因为它已经是 Object 的一个实例),它不可能有自己的(新的)作用域.它是高级 JS 编程的基本概念.拥有一个新的范围可以让你做几乎所有的事情(你可以声明你自己的 windowdocument 或任何你想要的,除了你的 JS keywords自己的范围).现在,举一些简单的例子:

There is a (fundamental, in my opinion) difference between object literals and functions, the "private" variables. Since an object can't be instantiated(because it is already an instance of Object) it has no possibility to have its own (new) scope. It is a base concept of advanced JS programming. Having a new scope allows you to do almost everything(you can declare your own window, document or whatever you want except the JS keywords inside your own scope). Now, some simple examples:

假设您想为同一对象创建大量实例(使用尽可能少的行):

Let's assume you want to create a large number of instances of the same object(using as few lines as possible):

function MyObj(i) {
    var privateCounter = "I am the instantiated object " + i + " .";
    this.counter = function() {
        return privateCounter;
    };
}

var MyObjList = [],
    ObjLitList = [];
for (var i = 0; i < 100; i++) {
    MyObjList.push(new MyObj(i));
    ObjLitList.push({counter: "I am the literal object number " + i + "."});
}

现在您有 200 个对象,它们几乎,但准确地说,不是同一回事.您可以根据需要扩展它们,因为函数对象,但是对于函数,您不能直接访问 private 变量.让我们看看函数有哪些优点:

Now you have 200 objects that are almost, but not precisely, the same thing. You can extend them as you prefer, because functions are objects, but in the case of the function you cannot access the private variable directly. Let's see which are the advantages of a function:

  • 它被当作一个Object
  • 它有自己的Prototype
  • 它有私有变量

还有Object?

  • 一个Object
  • 它没有自己的Prototype,但你可以声明函数并扩展对象本身
  • 它没有私有变量
  • It is an Object
  • It doesn't have its own Prototype, but you can declare the functions and extend the object itself
  • It doesn't have private variables

除了私有变量,它们彼此之间没有太大区别.

Apart the private vars, they are not much different from each other.

让我们看看函数的原型可以做什么:

Let's see what a function's prototype can do:

MyObj.prototype.setX = function(x) {
    this.x = x;
}

使用原型允许您创建一个匿名函数(也可以命名然后分配)的唯一实例,该实例将在实例之间共享.你怎么能用对象字面量做同样的事情?

Using the prototype allows you to create an only instance of an anonymous function(which can be named too and then assigned) which will be shared across instances. How can you do the same thing with object literals?

function setX(x) {
    this.x = x;
}
var obj = {
    setX: setX
};

如您所见,您必须创建对象,每次定义一个 setX 属性.否则,您可以扩展 Object.prototype 本身(但关于扩展原生 JS 对象的原型存在长期争论).

As you can see you have to create the object defining everytime a property which is setX. Otherwise, you can extend Object.prototype itself(but there is a long debate about extending the prototype of native JS objects).

那么哪种最佳方式是?没有一个,这取决于你必须做什么,你需要从你的脚本中得到什么,你觉得两者中的哪一个更舒服.

So which is the best way? There is no one, it depends on what you have to do, what you need from your script, which of the two you feel more comfortable with.

我更喜欢编写自己的函数并将它们视为类,因为它们更具可读性并且我可以使用私有"变量.我不知道有人使用文字而不是函数.

I prefer writing my own functions and treat them like classes, because they are more readable and I am able to use "private" variables. I don't know anyone using literals instead of functions though.

关于问题:

哪种编程方式最好(对象字面量 vs 构造函数 vs 原型)

Which is the best preferred way of programming(object literals vs constructors vs prototype)

回答.

是否可以只使用对象字面量而不使用构造函数和原型来编写带有构造函数和原型的代码.

can a code with constructor and protoype be written using just object literals without using constructor and protoype.

是的,如果你不需要私有变量,你可以(如果脚本不是太大.想象一下 jQuery 写成一个对象字面量 :D).

Yes, you can if you don't need private variables(and if the script isn't too big. Imagine jQuery written as an Object literal :D).

匿名函数的意义是什么.

what is the signifiance of anonymous function.

好吧,我可以举个例子来回答:

Oh well, I can answer with an example:

//code
myNamedFunction();
//code
function myNamedFunction() {
    alert("I'm defined everywhere! :)");
}

这有效并且不会产生 TypeError.

This works and won't generate a TypeError.

myAnonymousFunction();
var myAnonymousFunction = function() {
    alert("I'm defined after this declaration :(");
}
myAnonymousFunction(); // works!

这将导致 Uncaught TypeError: undefined is not a function,因为 myAnonymousFunction 只是对有效函数的引用(即未命名,因此无法从脚本中调用).

This will cause a Uncaught TypeError: undefined is not a function, because myAnonymousFunction is only a reference to the effective function(which is unnamed, so it is not callable from the script).

关于这个论点有很多话要说,开始高级编程的一个好点是Javascript 花园.其他不错的读物是 JS 中的 OOP 基础 -NetTutsPlus使用对象 - MDNJS 中的 OOP - Phrogz

There are a lot of things to say about this argument, and a good point to start advanced programming is Javascript Garden. Other good readings are Basics of OOP in JS - NetTutsPlus, Working with Objects - MDN and OOP in JS - Phrogz

希望这有帮助!

旁注:函数也有一个很好的优势,因为它们可以通过一个函数(例如call)改变它们的上下文(this),而对象不能.

Sidenote: functions also have a good advantage since they can change their context(this) just with a function(call for example), while objects can't.

这篇关于对象字面量 vs 构造函数+原型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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