Although many languages claim: "Everything is an object", in JavaScript, not all values are objects.
Primitive values vs objects
Values in JavaScript can be divided into two major categories: primitive values (primitive) and objects (object).
Definition
Definition of two values in JavaScript:
The values below are the original values.
1. String
2. Number: All numbers in JavaScript are floating point numbers
3. Boolean value
4.null
5.undefined
All other values are objects. Objects can be further divided:
1. Wrapper of original value: Boolean, Number, String. Rarely used directly.
2. Objects created with literals. The following literals produce objects, which can also be created through constructors. You can create objects using literals.
•[] is new Array()
•{} is new Object()
•function() {} is new Function()
•/s*/ is new RegExp("\ s*")
3. Date: new Date("2011-12-24")
Differences
You can define primitives and objects via enumerated primitives and non-primitives. But you can also describe what the primitives and objects are. Let's start with objects.
1. Objects are mutable:
> var obj = {};
> ; obj.foo = 123; // Add attributes and values
123
> obj.foo // Read attributes and return attribute values
123
2. Each object has its own unique identifier, so objects created through literals or constructors are not equal to any other objects, and we can compare them through ===.
> {} === {}
false
Objects are compared by reference. Only two objects with the same identifier are considered equal.
> var obj = {};
> ; obj === obj
true
3. Variables save references to objects, so if two variables apply to the same object - when we change one of the variables, both will also change.
> var var1 = {};
> ; var var2 = var1;
> var1.foo = 123; // Modify the properties of variable val1
123
> var2.foo // val2 also changed
123
As expected, the primitive value is not the same as the object:
1. Primitive values are immutable; you cannot add attributes to them:
> var str = "abc";
> str.foo = 123; // Add attributes (this operation will be ignored)
123
> str.foo // Read the value of the attribute and return undefined
undefined
2. The original value has no internal identifier, and the original value is compared by value: The basis for comparing two original values is their content. If the contents of the two original values are the same, the two original values are considered to be the same.
> "abc" === "abc"
true
这意味着,一个原始值的标识就是它的值,javascript 引擎没有为原始值分配唯一标识。
最后两个事实结合起来的意思是:我们无法区分一个变量到底是对象的引用,还是原始值的副本。
陷阱:原始值和它们的包装类型
规则:忽略尽可能多的包装类型。 在其他编程语言如Java,你很少会注意到他们。
原始值类型 boolean, number 以及 string 都有自己对应的包装类型 Boolean, Number 和 String。 包装类型的实例都是对象值,两种类型之间的转换也很简单:
•转换为包装类型:new String("abc")
•转换为原始类型:new String("abc").valueOf()
原始值类型以及它们相应的包装器类型有很多不同点,例如:
> typeof "abc"
'string'
> typeof new String("abc")
'object'
> "abc" instanceof String
false
> new String("abc") instanceof String
true
> "abc" === new String("abc")
false
包装类型的实例是一个对象,因此和 JavaScript 和对象一样,包装类型也无法进行值的比较(只能比较引用)。
> var a = new String("abc");
> var b = new String("abc");
> a == b
false // 虽然 a 和 b 有相同的内容,但是依然返回 false
> a == a
true
原始值没有自己的方法
包装对象类型很少被直接使用,但它们的原型对象定义了许多其对应的原始值也可以调用的方法。 例如,String.prototype 是包装类型 String 的原型对象。 它的所有方法都可以使用在字符串原始值上。 包装类型的方法 String.prototype.indexOf 在 字符串原始值上也有,它们并不是两个拥有相同名称的方法,而的的确确就是同一个方法:
> "abc".charAt === String.prototype.charAt
true
在数字的包装类型 Number 的原型对象有 toFixed 方法,即 Number.prototype.toFixed,但是当我们写如下代码时却发生错误:
> 5.toFixed(3)
SyntaxError: Unexpected token ILLEGAL
此错误是解析错误(SyntaxError),5 后面跟着一个点号(.),这个点被当作了小数点,而小数点后面应该是一个数,以下代码可以正常运行:
> (5).toFixed(3)
"5.000"
> 5..toFixed(3)
"5.000"
值的分类:typeof 和 instanceof
如果你想要对值进行分类,你需要注意原始值和对象之间的区别。 typeof 运算可以用来区分原始值和对象。instanceof 可以用来区分对象,而且,instanceof 对于所有的原始值都返回 false。
typeof
typeof 可以用来判断原始值的类型,以及区分对象值和原始值:
> typeof "abc"
'string'
> typeof 123
'number'
> typeof {}
'object'
> typeof []
'object'
typeof 返回以下字符串:
参数 | 结果 |
---|---|
undefined | "undefined" |
null | "object" |
布尔值 | "boolean" |
数字 | "number" |
字符串 | "string" |
函数 | "function" |
其他 | "object" |
Note:
•typeof will return "object" when operating on null, which is a bug in the JavaScript language itself. Unfortunately, this bug will never be fixed because too much existing code already relies on this behavior. This does not mean that null is actually an object[4].
•typeof also allows you to check whether a variable has been declared without throwing an exception. No function can do this because you cannot pass an undeclared variable to a function parameter.
> typeof undeclaredVariable
'undefined'
> undeclaredVariable
ReferenceError: undeclaredVariable is not defined
• Functions are also object types; this may not be understood by many people, but sometimes it is very useful.
•An array is an object.
More information about typeof [5] and [6].
instanceof
instanceof can detect whether a value is an instance of a constructor:
value instanceof Constructor
If the above expression returns true, it means value is an instance of Constructor. It is equivalent to:
Constructor.prototype.isPrototypeOf(value)
Most objects are instances of Object because the end of the prototype chain is Object.prototype. The primitive value is not an instance of any object:
> "abc" instanceof Object
false
> "abc" instanceof String
false

去掉重复并排序的方法:1、使用“Array.from(new Set(arr))”或者“[…new Set(arr)]”语句,去掉数组中的重复元素,返回去重后的新数组;2、利用sort()对去重数组进行排序,语法“去重数组.sort()”。

本篇文章给大家带来了关于JavaScript的相关知识,其中主要介绍了关于Symbol类型、隐藏属性及全局注册表的相关问题,包括了Symbol类型的描述、Symbol不会隐式转字符串等问题,下面一起来看一下,希望对大家有帮助。

怎么制作文字轮播与图片轮播?大家第一想到的是不是利用js,其实利用纯CSS也能实现文字轮播与图片轮播,下面来看看实现方法,希望对大家有所帮助!

本篇文章给大家带来了关于JavaScript的相关知识,其中主要介绍了关于对象的构造函数和new操作符,构造函数是所有对象的成员方法中,最早被调用的那个,下面一起来看一下吧,希望对大家有帮助。

本篇文章给大家带来了关于JavaScript的相关知识,其中主要介绍了关于面向对象的相关问题,包括了属性描述符、数据描述符、存取描述符等等内容,下面一起来看一下,希望对大家有帮助。

方法:1、利用“点击元素对象.unbind("click");”方法,该方法可以移除被选元素的事件处理程序;2、利用“点击元素对象.off("click");”方法,该方法可以移除通过on()方法添加的事件处理程序。

本篇文章给大家带来了关于JavaScript的相关知识,其中主要介绍了关于BOM操作的相关问题,包括了window对象的常见事件、JavaScript执行机制等等相关内容,下面一起来看一下,希望对大家有帮助。

foreach不是es6的方法。foreach是es3中一个遍历数组的方法,可以调用数组的每个元素,并将元素传给回调函数进行处理,语法“array.forEach(function(当前元素,索引,数组){...})”;该方法不处理空数组。


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

SublimeText3 Chinese version
Chinese version, very easy to use

SublimeText3 Mac version
God-level code editing software (SublimeText3)

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

Dreamweaver CS6
Visual web development tools

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software
