In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly introduces how to obtain attributes in javascript, which is very detailed and has certain reference value. Friends who are interested must finish reading it!
Javascript access to attributes: 1, to create an object construction method "function myObj (name, attr) {...}"; 2, through the "for (var i in myTester) {...}" method to get the object method.
This article operating environment: windows7 system, javascript1.8.5 version, Dell G3 computer.
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 properties: name object properties: attr above is all the content of the article "how to get properties in javascript". Thank you for reading! Hope to share the content to help you, more related 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.