简体   繁体   English

MDN为什么说“ JavaScript中的一切都是对象”?

[英]Why does MDN say “everything is an object in JavaScript”?

On an MDN page about string methods ( https://developer.mozilla.org/en-US/docs/Learn/JavaScript/First_steps/Useful_string_methods ), it says "We've said it before, and we'll say it again — everything is an object in JavaScript." 在有关字符串方法的MDN页面上( https://developer.mozilla.org/en-US/docs/Learn/JavaScript/First_steps/Useful_string_methods ),其上显示“我们之前已经说过,我们将再说一遍— 一切都是JavaScript中的对象。” But another MDN page states there are 7 data types in JavaScript, 6 primitives and object ( https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Data_types ). 但是另一个MDN页面指出,JavaScript中有7种数据类型,6种原语和对象( https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Data_types )。 This is simply a mistake, correct? 这只是一个错误,对吗? Or is there something I'm missing? 还是我想念的东西?

My terminology may be off, and I'm not one to pay attention to details like these, but maybe this helps clarify it a bit. 我的术语可能不正确,我不是要注意这些细节的人,但这也许可以帮助您澄清一下。

For example, number variables have the toExponential method. 例如,数字变量具有toExponential方法。 Eg, this is invalid 3.toExponential(2) , but this is totally valid let a = 3; a.toExponential(2); 例如,这是无效的3.toExponential(2) ,但这是完全有效的, let a = 3; a.toExponential(2); let a = 3; a.toExponential(2); . On the other hand, both typeof 3 and typeof a returns the string 'number' . 另一方面, typeof 3typeof a返回字符串'number'

So there's a distinction between what's an 'object' (roughly, what can have methods) and the data type a variable or literal can reference. 因此,什么是“对象”(大致上可以有方法)和变量或文字可以引用的数据类型之间是有区别的。

I think it is a poor choice of phrasing. 我认为这是措辞不佳的选择。 Things that are passed by reference are objects (array, or plain object, functions etc). 通过引用传递的东西是对象(数组或普通对象,函数等)。 The primitive data types are passed by value and I would consider null to be a keyword (it is also an object). 原始数据类型通过值传递,我认为null为关键字(它也是一个对象)。 The correct phrase would be: 正确的短语是:

"Everything can be represented as an object" “一切都可以表示为对象”

This would clear the misconception, as others pointed out, the only way you can call functions from Number.prototype or String.prototype is for the engine to implicity wrap the primitive as an object. 就像其他人指出的那样,这将消除误解,从Number.prototype或String.prototype调用函数的唯一方法是引擎隐式将原语包装为对象。 The same way, you can do this (function(){console.log(this)}).call(5) //Number {5} 同样,您可以执行此操作(function(){console.log(this)}).call(5) //Number {5}

every data type is instance of it's own class. 每个数据类型都是它自己的类的实例。 like for example string. 例如字符串。 any string is instance of Class String all instance inherits methods from String class. 任何字符串都是String类的实例,所有实例均从String类继承方法。 methods are available only to object so you can consider that string is object to. 方法仅对对象可用,因此您可以认为该字符串是对象。 but for sake continence all datatypes exists.. 但是为了节制,所有数据类型都存在。

i tried to explain the concept but it's complicated 我试图解释这个概念,但是它很复杂

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

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