Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use the built-in object of JavaScript

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

Most people do not understand the knowledge points of this article "how to use JavaScript's built-in objects", so the editor summarizes the following content, detailed content, clear steps, and has a certain reference value. I hope you can get something after reading this article. Let's take a look at this "how to use JavaScript's built-in objects" article.

1. The basis of the object

1.1 Typ

JavaScript has six main language types:

String, number, boolean, undefined, null, object

Base type: the string,number,boolean,undefined,null; base type itself is not an object.

Null

But null is sometimes treated as an object and typeof null returns object. In fact, null is the basic type. The reason is that different objects are represented as binary at the bottom. In JavaScript, if the first three binary bits are 0, they will be judged as object type, and null represents all zeros, so object will be returned when typeof.

Special object subtype

Array

Arrays are also a type of objects with some additional behavior. Arrays are organized in a more complex way than normal objects.

Function

Functions are essentially the same as ordinary functions, except that they can be called, so they can operate like objects.

1.2 built-in object

String

Number

Date

Boolean

Object

Function

Array

1.3 content

.an is called attribute access, and ['a'] is called operator access.

The property name in the / / object is always a string

Myobj= {}

Myobj [myobj] = 'bar'// assignment

Myobj ['[object object]'] / / 'bar'

1.4 computable attribute names

Es6 adds computable attribute names. You can use [] to wrap an expression as an attribute name in literal form.

Var perfix = 'foo'

Var myobj= {

[perfix + 'bar']:' hello'

}

Myobj ['foobar'] / / hello

1.5 attribute descriptor

Starting with es5, all attributes have attribute descriptors, such as you can directly determine whether the attribute is readable or writable.

/ *

* important functions:

* Object.getOwnPropertyDescriptor () / / get the attribute descriptor

* Object.defineProperty () / / sets the property descriptor

, /

Writeble (readability)

Configurable (Configurability)

Enumerable (enumerable)

1.6 traversal

For in

For in can be used to traverse the list of enumerable properties of an object (including the [[Prototype]] chain), and you need to get the property values manually. You can traverse arrays and ordinary objects

For of

Es6 is added, which can be used to traverse the property values of an array. The for of loop first requests an iterator object from the accessed object, and then iterates through all the return values by calling the iterator object's next () method.

The array has a built-in @ @ iterator

How does for of work?

Var arr = [1,2,3]

Var it = arr [Symbol.iterator] () / / iterator object

Console.log (it.next ()); / / {value: 1, done: false}

Console.log (it.next ()); / / {value: 2, done: false}

Console.log (it.next ()); / / {value: 3, done: false}

Console.log (it.next ()); / / {value: undefined, done: true}

/ *

* use the Symbol.iterator of es6 to get the iterator internal properties of the object.

* @ @ iterator itself is not an iterator object, but a function that returns an iterator object.

, /

How does an object build @ @ iterator to traverse the value of a property?

The for cannot be completed automatically because the object does not have a built-in @ @ iterator. Of traversal. However, you can give you any object definition you want to traverse @ @ iterator, for example:

Var obj= {

A:1,b:2

}

Object.defineProperty (obj, Symbol.iterator, {

Enumerable: false

Writable: false

Configurable: true

Value: function () {

Var self = this

Var idx = 0

Var ks = Object.keys (self)

Return {

Next: function () {

Return {

Value: Self [KS [IDX + +]]

Done: (idx > ks.length)

}

}

}

}

})

/ / manually traverse

Var it = obj [Symbol.iterator] () / / iterator object

Console.log (it.next ()); / / {value: 1, done: false}

Console.log (it.next ()); / / {value: 2, done: false}

Console.log (it.next ()); / / {value: undefined, done: true}

/ / for of traversal

For (const v of obj) {

Console.log (v)

}

/ / 2

/ / 3

Other array traversal functions

/ *

ForEach: traverses all and ignores the return value

Some: runs until the callback function returns true (or "true" value)

Every: runs until the callback function returns false (or "false" value)

Map:

Filter: returns the value that meets the condition

Reduce:

Some is similar to the break statement of every and for, which terminates the traversal in advance.

, /

The above is about the content of this article on "how to use JavaScript's built-in objects". I believe we all have a certain understanding. I hope the content shared by the editor will be helpful to you. If you want to know more about the relevant knowledge, please follow the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report