Javascript 对象与原始

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8643276/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 06:52:39  来源:igfitidea点击:

object vs. primitive

javascriptoopobjectprimitive

提问by Zach Shallbetter

Recently had an interviewer ask to define the difference between objects and primitives. Seemed like an odd question considering that all languages begin with a primitive. How would you have answered this question?

最近有一位面试官要求定义对象和基元之间的区别。考虑到所有语言都以原始语言开头,这似乎是一个奇怪的问题。你会如何回答这个问题?

I should also note that this interview was for a front-end development position so the language (I assume) he was referring to was JavaScript.

我还应该指出,这次面试是针对前端开发职位的,所以他所指的语言(我假设)是 JavaScript。

回答by Mithun Sasidharan

A primitive is a data type that is composed of no other data types and can not be broken down any further. It is like the atoms in the programming scenario. I say atom because atom is a basic unit of matter and there is nothing that can be derived from it.

原语是一种数据类型,它不由其他数据类型组成,不能进一步分解。它就像编程场景中的原子。我说原子是因为原子是物质的基本单位,没有任何东西可以从它派生出来。

I mean, an intin C can not be broken down into smaller data type. An object, on the other hand can be thought of a molecule, consisting of more than one primitive type. For example, stringcomes as part of the C++ standard library; however, it is an object and it is composed of smaller data types internally and contains methods.

我的意思是,intC 中的an不能分解为更小的数据类型。另一方面,对象可以被认为是由多个原始类型组成的分子。例如,string作为 C++ 标准库的一部分;然而,它是一个对象,它在内部由较小的数据类型组成并包含方法。

It is important to note that not all object-oriented languages are class based (eg. Javascript) You can not define a class in Javascript, so an object is quite different here. Even though everything in Javascript is an object (Ruby also), the Number object is really a wrapper for an internal primitive.

需要注意的是,并非所有面向对象的语言都是基于类的(例如 Javascript)。您不能在 Javascript 中定义类,因此这里的对象完全不同。尽管 Javascript 中的一切都是对象(Ruby 也是),但 Number 对象实际上是内部原语的包装器。

回答by Bhushan

From Javaperspective:

Java 的角度来看:

  1. A primitive is not composed of other data types. Where as an object can be and generally is.
  2. Primitives are passed by value, i.e. a copy of the primitive itself is passed. Whereas for objects, the copy of the reference is passed, not the object itself.
  3. Primitives are independent data types, i.e. there does not exist a hierarchy/super class for them. Whereas every Object is descendent of class "Object".
  4. Every object has some default methods of itself, which it inherits from the class Object (e.g. toString(), clone(), wait(), notify(), notifyAll(), etc.). The primitives does not have any method associated with themselves.
  5. Primitives are immutable, they can be used as so without any special care. Whereas for objects, special care needs to be taken, they are not immutable by default.
  6. With objects, there are two types of copies, Shallow and Deep. There is a significant difference between them. So the choice depends on how do we intend to use them. With primitives, there is no such difference, everything is by default deep copy only.
  1. 原语不由其他数据类型组成。一个对象可以在哪里并且通常在哪里。
  2. 原语按值传递,即传递原语本身的副本。而对于对象,传递的是引用的副本,而不是对象本身。
  3. 原语是独立的数据类型,即它们不存在层次结构/超类。而每个对象都是“对象”类的后代。
  4. 每个对象都有一些自己的默认方法,这些方法是从类 Object 继承而来的(例如 toString()、clone()、wait()、notify()、notifyAll() 等)。原语没有任何与它们相关联的方法。
  5. 原语是不可变的,它们可以直接使用而无需任何特别注意。而对于对象,需要特别注意,默认情况下它们不是不可变的。
  6. 对于对象,有两种类型的副本,浅副本和深副本。它们之间存在显着差异。所以选择取决于我们打算如何使用它们。对于原语,没有这样的区别,默认情况下一切都是深拷贝。

回答by htet thiri naing

I think primitive cannot divided any more like int, string (such as built-in data type) . On the other way, object can divide into small pieces like array, structure.

我认为primitive不能再像int、string(比如内置数据类型)那样划分了。另一方面,对象可以分成小块,如数组、结构。

I am just a student and this is my opinion.
If you think my answer is wrong, you can correct me.
Thanks

我只是一个学生,这是我的意见。
如果您认为我的回答有误,您可以纠正我。
谢谢

回答by N.Neupane

Let us Visualize the actual differences.

让我们想象一下实际的差异。

Primitives

原语

1) A primitive data type uses a small amount of memory to represent a single item of data. All data of the same primitive type are the same size.

1) 原始数据类型使用少量内存来表示单个数据项。相同原始类型的所有数据大小相同。

For Example: Primitive type int represents integers using 32 bits. All variables of type int use 32 bits.

例如:原始类型 int 使用 32 位表示整数。所有 int 类型的变量都使用 32 位。

2) There are only eight primitive data types in Java: byte, short, int, long, float, double, char, and boolean. A Java program cannot define any other primitive data types.

2)Java中只有八种原始数据类型:byte、short、int、long、float、double、char和boolean。Java 程序不能定义任何其他原始数据类型。

Objects

对象

An object is a large chunk of memory that can potentially contain a great deal of data along with methods (little programs) to process that data. There are thousands of object classes that come standard with Java, and a programmer can easily create additional classes. (Although there are thousands of standard classes, for this course you only need become familiar with a dozen or so classes.)

对象是一大块内存,可能包含大量数据以及处理该数据的方法(小程序)。Java 标配了数千个对象类,程序员可以轻松创建其他类。(虽然有成千上万的标准课程,但对于本课程,您只需要熟悉十几个课程即可。)