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 does Javascript get the object?

2025-03-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

This article mainly introduces "how to get objects by Javascript". In daily operation, I believe many people have doubts about how to obtain objects by Javascript. The editor consulted all kinds of materials and sorted out simple and easy-to-use methods of operation. I hope it will be helpful for you to answer the doubts about "how to get objects by Javascript". Next, please follow the editor to study!

The method of Javascript to get the object: 1, the construction method of creating an object through function; 2, creating an object through new; 3, obtaining the properties and methods defined directly on the object.

This article operating environment: windows7 system, javascript1.8.5 version, DELL G3 computer

How does Javascript get the object?

JavaScript gets object properties and methods

First, get object properties and methods

Object.keys () returns an array of enumerable properties and methods of the object.

All the properties of the array returned by Object.getOwnPropertyNames () (enumerable or non-enumerable) find the given object directly.

/ / create the constructor of an object function myObj (name, attr) {this.name = name; this.attr = attr; this.sayHi = function () {return'hi everyoneconstructors }} / / create an object var myTester = new myObj ("shinejaie", 1) / / get the properties and methods that are directly defined (enumerable) on the object var arr = Object.keys (myTester); console.log ('arr', arr) / / output all the attributes (enumerable or non-enumerable) of the array returned by arr ["name", "attr", "sayHi"] / / to find the given object directly. Console.log ("attr", Object.getOwnPropertyNames (myTester)); / / output attr ["name", "attr", "sayHi"] / / add an attribute Object.prototype.newShine = "it's me" to the Object prototype; / / return the enumerable property that keeps finding the object's prototype chain for (var i in myTester) {console.log (I) } / / output name,attr,sayHi,newShine / / returns the enumerable attribute for (var i in myTester) {if (myTester.hasOwnProperty (I)) {console.log (I);}} / / output name,attr,sayHi defined directly on the object

2. Object.keys (), Object.getOwnPropertyNames (), for...in... Contrast

/ / non-enumerable object properties var nonenum = Object.create ({}, {getFoo: {value: function () {return this.foo;}, enumerable: false}}); nonenum.foo = 1; nonenum.asj = 2 / / get the enumerable or non-enumerable properties of the object console.log (Object.getOwnPropertyNames (nonenum) .sort ()); / / output ["asj", "foo", "getFoo"] / / get the enumerable properties of the object console.log (Object.keys (nonenum) .sort ()) / / output ["asj", "foo"] / / returns enumerable properties for (var i in nonenum) {if (nonenum.hasOwnProperty (I)) {console.log (I); / / output foo asj} defined directly on the object

3. Get the property name and method name of JavaScript object respectively

/ / create the constructor of an object function myObj (name, attr) {this.name = name; this.attr = attr; this.sayHi = function () {return'hi everyoneconstructors }} / / create an object var myTester = new myObj ("shinejaie", 1) / / get the object method for (var i in myTester) {if (myTester.hasOwnProperty (I) & & typeof myTester [I] = "function") {console.log ("object method:", I, "=") MyTester [I])}} / / output object method: sayHi = () {return'hi everyoneObjective } / / get object property for (var i in myTester) {if (myTester.hasOwnProperty (I) & & typeof myTester [I]! = "function") {console.log ("object property:", I) }} / / output object attribute: name object property: attr so far, the study on "how to get the object by Javascript" is over. I hope you can solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

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