In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)05/31 Report--
This article focuses on "how to customize objects in JavaScript". Interested friends may wish to take a look. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn how to customize objects in JavaScript.
Custom method: 1, directly through the "attribute name / value" to create, syntax "var object name = {attribute name: attribute value};"; 2, use "var object name = new constructor name (args)" statement; 3, use "Object.create (prototype object, descriptors)" statement.
The operating environment of this tutorial: windows7 system, javascript1.8.5 version, Dell G3 computer.
In Js, in addition to Array, Date, Number and other built-in objects, developers can create their own objects through Js code.
Object Properti
The ① structure is similar to a dictionary: the property of an object is similar to a key / value pair; the name of the attribute is a string, and the value of the attribute is of any type.
② prototype inheritance: the object of Js inherits the properties of the prototype.
③ dynamic structure: the attributes of objects can be dynamically added or deleted.
④ reference type: objects in js are reference types. An is an object, breada, and modifying b will also cause a modification.
How to create objects
Custom objects are created in Js in three ways: object literals, new constructors, and the Object.create () method. Each creation method inherits a different prototype object:
Direct amount of ① object: the prototype is Object.prototype.
② new constructor: the prototype is the prototype property of the constructor.
③ Object.create (): the prototype is the first parameter passed in. If the first parameter is null, take Object.prototype as the prototype.
1. Direct quantity of object
Description: created directly through the attribute name / value.
Syntax: var o = {name:'tom', age:22}
Prototype: Object.prototype
Applicable scenario: applied in a specific scope.
Example:
Var o = {name: 'tom'} console.log (o.constructor.prototype); / / = > Object (): the prototype of the direct quantity of the object is Objectconsole.log (o.constructor.prototype = = Object.prototype); / / true
2. New constructor
Description: the constructor is also a kind of function, but in order to distinguish the commonly used functions, the name of the constructor is written in the big camel hump (the first letter is capitalized).
Syntax: var o = new ClassName ()
Prototype: the prototype property of the constructor.
Example:
/ / 1. Create the constructor function People (name) {this.name;} var p = new People ('Tom'); console.log (p.constructor.prototype); / / = > People {}: prototypeconsole.log (p.constructor.prototype = People.prototype) whose prototype is the constructor; / / = > true// 2. Multi-level inheritance of custom objects: constructor returns the first called constructor function Student (age) {this.age = age;} Student.prototype = new People (); / / sets the prototype of Student to People object var s = new Student (22); / / calls People () before Student () console.log (s.constructor) when the object is initialized / / = > function People: the constructor returned by object s is Peopleconsole.log (s.constructor.prototype); / / = > People {}: the prototype object is Peopleconsole.log (s.constructor.prototype = People.prototype); / / = > true
3. Object.create (prototype, propertyDescriptor): ECMAScript 5 specification
Description: creates and returns an object that specifies the prototype and the specified properties.
Syntax: Object.create (prototype, propertyDescriptor)
Parameters:
① prototype {prototype}: create a prototype of the object, which can be null. If null, the prototype of the object is undefined.
② propertyDescriptor {propertyDescriptor} optional: attribute descriptor.
Prototype: the prototype is ①; if the ① parameter is null, the prototype of the object is undefined.
Example:
/ / 1. Create a prototype object var o = Object.create (null, {name: {value: 'tom'}}); console.log (o.constructor); / / = > undefined / / 2. Create a prototype object var array = Object.create (Array.prototype, {}); console.log (array.constructor); / / = > function Array constructor console.log (array.constructor.prototype); / / = > []: the prototype object is an array / / 3. Create a prototype for the custom class object function People () {} var p = Object.create (People.prototype, {}); console.log (p.constructor); / / = > function People constructor console.log (p.constructor.prototype); / / = > People {}: prototype object People so far, I believe you have a deeper understanding of "JavaScript how to customize objects", might as well come to the actual operation! Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.