In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-26 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
Editor to share with you the example analysis of Object objects in JS, I believe that most people do not know much about it, so share this article for your reference, I hope you can learn a lot after reading this article, let's go to know it!
Object concept
In javascript, all reference types are objects. As in function Foo () {}, Foo itself is a reference to an object.
Create object mode literal quantity mode new constructor declaration Object.create
Literal object
An instance of quickly creating objects at the javascript language level
Var obj = {foo: 'foo', bar:' bar'}; / / Object object literals var obj2 = [obj, 'foo',' bar']; / / Array array literals var obj3 = / ^ [a-zA-Z0-9] $/; / RegExp regular literals var obj4 = function () {}; / / Function function digits
New constructor
Through the constructor of the built-in object, or a custom function. Using the new operator, create an object and execute the constructor method.
Var obj = new Object (); var obj2 = new Array (1000); var obj3 = new RegExp ('^ [a-zA-Z0-9] $'); var obj4 = new Function ('return a + b')
Function declaration
The function declares the object created. Functions belong to special objects.
Function Foo () {} Foo instanceof Object;Foo instanceof Function
Object.create
Pass in an object as the prototype of the returned object, create a new object, and point the prototype of the new object to the incoming object.
Var foo = {'foo':' foo', 'bar':' bar'}; var o = Object.create (foo); / / o.protoplast _ = fooconsole.log (o.foo); / / o.__proto__.foo
Use Object.create (null) to return a typical object.
Var o = Object.create (null); o instanceof Object; / / return false;o.toString (); / / Uncaught TypeError
Object prototype
Each object has a built-in _ _ proto__ attribute that points to the function prototype property that constructed it. And the constructor's
Prototype.constructor points to the constructor itself. The process of finding the properties of an object consists of the following parts:
Look for the data descriptor (writable, value) or access descriptor (getter, setter) of the object property, and return the corresponding value if the query is found. If the query cannot be found, proceed to step 2. Look for whether the value of the object property is detected by the display definition (which can be detected by Object.getOwnPropertyNames), and if the object property is defined, the defined value is returned. If not, proceed to step 3. Find the properties of the hidden prototype _ _ proto__ object of the object, the rules are the same as step 1 and step 2. If not, repeat step 3 until _ _ proto__ is null.
The specific example is shown in the following figure:
Detection object prototype
Test whether an object has a prototype property of a constructor in its prototype chain
Instanceof Object.prototype.isPrototypeOf
Instanceof
Operator at the language level to detect whether the prototype chain of the object contains the prototype of the constructor
Function Foo () {} Foo instanceof Function; / / return trueFoo instanceof Object; / / return true
To simulate whether the constructor of the prototype chain of the instanceof lookup object contains the passed constructor, _ _ proto__ is exposed to the user in some specific browsers.
Function Bar () {} function isInstanceof (obj, Constructor) {if (obj = = null) {return false;} / / ignore string, number, boolean, null, undefined types interfere with if (! ~ ['object',' function'] .indexOf (typeof obj)) {return false;} var prototype = obj.__proto__; while (prototype) {if (prototype.constructor = Constructor) {return true;} prototype = prototype.__proto__;} return false;} isInstanceof (Bar, Function) IsInstanceof (Bar, Object)
IsPrototypeOf
The function property of the prototype object of the constructor is used to detect whether the prototype object of the constructor exists in the prototype chain of the target object.
Function Baz () {} var baz = new Baz (); Baz.prototype.isPrototypeOf (baz); Function.prototype.isPrototypeOf (baz); Object.prototype.isPrototypeOf (baz)
Get object prototype Object.getPrototypeOf _ _ proto__
Var o = {}; var prototype = Object.getPrototypeOf (o); console.log (prototype = Object.prototype); / / return true// some browsers valid var O2 = {}; console.log (o2.roomprotoplast = = Object.prototype); / / return true
Set object prototype Object.create Object.setPrototypeOf
Object.create
Return an object and set its prototype
Function Foo () {} function Bar () {} Foo.prototype.foo = 'foo'; Bar.prototype = Object.create (Foo.prototype); Bar.prototype.constructor = Bar; / / modify constructor var o = new Bar () of the prototype chain; console.log (o.foo); / / return foo; console.log (o instanceof Bar); / / return true
Object.setPrototypeOf
Directly set the implicit prototype of the object _ _ proto__
Function Foo () {} Foo.prototype.name = 'foo'; var o = Object.create (null); Object.setPrototypeOf (o, Foo.prototype); console.log (o.name); / / return foo is all the contents of the article "sample Analysis of Object objects in JS". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to 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.
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.