为什么JavaScript Primitive-Variables不可变和对象变量不? [英] Why are JavaScript Primitive-Variables immutable and Object-Variables not?

查看:49
本文介绍了为什么JavaScript Primitive-Variables不可变和对象变量不?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有一种方法可以在prototype-property的帮助下将一个成员函数或成员属性添加到Number,String,ect ...- Variables:

There is a way to add a member-function or member-property to Number, String, ect...-Variables with the help of the prototype-property:

 Number.prototype.member = function(){ console.log('number-member-function called'); };

或借助变量本身的proto-property:

or with help of the proto-property of the variables themselves:

 var num = 7;
 num.__proto__.member = function(){ console.log('number-member-function called'); };

就像任何其他类型的JavaScript类型一样。
但是,JavaScript中的Primtives和Objects的实现有什么不同,以便下面的代码不适用于Numbers而不适用于Objects?

Just like to any other kind of JavaScript-types. But what is the difference of the implementation of Primtives and Objects in JavaScript so that the code below does not work for Numbers but for Objects?

 var num = 7;
 num.member = function(){ console.log('number-member-function called'); };
 num.member(); // TypeError: num.member is not a function

 var obj = {};
 obj.member = function(){ console.log('object-member-function called'); };
 obj.member(); // object-member-function called

是否有人知道JavaScript Primitves和对象是如何在油烟机?因为所有浏览器中的所有JavaScript实现必须完全相同或几乎完成,因为使用名为member的Number-Function抛出错误。

Does anybody know approximatly how JavaScript Primitves and Objects are implemented under the hood? Because all JavaScript-implementations in all browsers must be done identically or almost for there is an error thrown with the Number-Function called member.

推荐答案

当您使用布尔字符串数字的文字符号时类型,不使用构造函数(布尔值,字符串,数字)。原始类型的行为几乎与使用 new 运算符实例化的一样,但它们是真正的原始类型。

When you use the literal notation for the boolean, string and number types, the constructors (Boolean, String, Number) are not used. The primitive types still behaves almost like they had been instantiated with the new operator, but are truly primitive types.

只有当您尝试将它们用作对象或者您尝试使用构造函数的属性时,JavaScript解释器才会在幕后创建一个包装器对象。
在此之后,包装器对象将被丢弃,您将再次处理基本类型。
所以:

Only when you are trying to use them as objects or you are trying to use a property of a constructor, the JavaScript interpreter creates a wrapper object around them behind the scenes. After this, the wrapper objects gets discarded and you are dealing again with the primitive type. So:

var num = 7; //primitive

num.member = function(){ console.log('number-member-function called'); }; //you are trying to use num as an object: a wrapper object gets created and discarded just after the assignment

//this will throw an error, because here num is primitive again (member was defined on the discarder wrapper)
num.member = function(){ console.log('number-member-function called'); };

关于这个主题的更多信息可以在Cody Lindley的JavaScript启蒙一书中找到,基于ECMA-262,第3版规范。

More on this topic can be found on the book "JavaScript Enlightenment" by Cody Lindley, based on the ECMA-262, Edition 3 specification.

这篇关于为什么JavaScript Primitive-Variables不可变和对象变量不?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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