Home > Web Front-end > JS Tutorial > JavaScript packaging object instance analysis_javascript skills

JavaScript packaging object instance analysis_javascript skills

WBOY
Release: 2016-05-16 16:07:15
Original
1138 people have browsed it

The examples in this article describe the usage of JavaScript packaging objects. Share it with everyone for your reference. The specific analysis is as follows:

A js object is a composite value: it is a property or a collection of named values.

Refer to the following code:

var s = "hello world";
var len = s.length;
Copy after login

In this example, s is a string, and string is not an object, but why does it have attributes? In fact, as long as the properties of the string s are referenced, js will convert the string into an object by calling new String(s). The object inherits the methods of the string and is used to handle the reference of the property; once the property is referenced At the end, the newly created object will be destroyed (this temporary object is not necessarily created or destroyed in implementation, but the whole process seems to be like this).

Like strings, numbers and Boolean values ​​also have their own methods: a temporary object is created through the Number() single-core Boolean() constructor, and the calls to these methods are all from this temporary object; however, null and undefined do not Wrapper objects: accessing their properties will cause a type error.
For example, the following code:

var s0 = "hello world";
s0.len = 100;
var t = s.len; //t的值将为undefined
Copy after login

Because line 2 creates a temporary object and destroys it immediately; line 3 creates a new string object through the original string value and tries to read its len attribute, which naturally does not exist. This code illustrates how to behave like an object when reading property values ​​or methods of strings, numbers, and Boolean values. But if you try to assign a value to its property, this operation will be ignored: the modification only occurs on the temporary object, and the temporary object is not retained.
Temporary objects created temporarily when accessing properties of strings, numbers, or Boolean values ​​are called wrapper objects.
We can explicitly create a string object and add its properties, and naturally the properties will be retained:

var str = "hello world";
var objs = new String(str);
objs.len = 100;
var t = objs.len; //t将被赋值为100
Copy after login

js will convert the wrapped object to a primitive value when necessary so the displayed object created and its corresponding primitive value often, but not always, behave the same. The == operator treats the original value and its wrapped object as equal; but the === congruence operator treats them as unequal; in addition, the typeof operator can be used to see the difference between the original value and the wrapped object.

I hope this article will be helpful to everyone’s JavaScript programming design.

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template