问题描述
最近有一位采访者要求定义对象和基元之间的区别。考虑到所有语言都以原语开头,这似乎是一个奇怪的问题。你会如何回答这个问题?
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?
我还应该注意到这次访谈是针对前端开发职位所以他所指的语言(我假设)是JavaScript。
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.
推荐答案
基元是一种数据类型,它不包含任何其他数据类型,不能再进一步细分。它就像编程场景中的原子。我说原子是因为原子是物质的基本单位,没有任何东西可以从中得出。
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.
我的意思是, int 无法细分为较小的数据类型。另一方面,物体可以被认为是由多于一种原始类型组成的分子。例如,
string
是C ++标准库的一部分;但是,它是一个对象,它由内部较小的数据类型组成,并包含方法。
I mean, an int
in 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, string
comes as part of the C++ standard library; however, it is an object and it is composed of smaller data types internally and contains methods.
重要的是要注意并非所有面向对象的语言都是基于类的(例如Javascript)你不能在Javascript中定义一个类,所以一个对象是完全不同的这里。即使Javascript中的所有内容都是对象(也是Ruby),Number对象实际上是内部基元的包装器。
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.
这篇关于对象与原语的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!