Get the App
SLTechnology News&Howtos  ›  Development  › 

How does Javascript get the object?

Shulou Source: shulou.com Published: 2022-06-02 14:51:21 10月15日 Update

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!

Tags: Objects properties methods output learning arrays prototypes more help utility next name article environment theory computer knowledge article system website Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Shulou Tech Info MariaDB Xiaomi vpn Linux