Go: JavaScript gets object properties and methods

Source: Internet
Author: User
Tags hasownproperty

first, Get Object properties and Methods Object.keys () for in returns an array of the names of the enumerable properties and methods of the object . all properties of the returned array ( enumerable or non-enumerable ) are directly found for the given Object. Object.getOwnPropertyNames() 
1//To create a method for constructing an object2functionMYOBJ (name, Attr) {3THIS.name =Name4This.attr =attr56This.sayhi =function() {7Return ' Hi everyone!!! ';8}9}1011//Create an Object12var mytester =New MYOBJ ("shinejaie", 1)13//Gets properties and methods that are defined (enumerable) directly on the object14var arr =Object.keys (mytester);Console.log (' arr ', arr);//Output arr ["name", "attr", "sayhi"]1617//All properties of the returned array (enumerable or Non-enumerable) are directly found for the given Object.Console.log ("attr", object.getownpropertynames (mytester));//Output attr ["name", "attr", "sayhi"]1920//Add a property to the Object prototypeObject.prototype.newShine = "it s me";2223//Returns an enumerable property that has been found in the prototype chain of the object24for (var iInchMytester) {25Console.log (i);26}27// output name,attr,sayhi,newshine28 29 // Returns the enumerable property directly defined on the Object. Span style= "color: #008080" >30 for (var i in mytester) {31 if (mytester.hasownproperty (i)) { 32  console.log (i);  }34 }35 // output name,attr,sayhi     
second, object.keys (), Object.getOwnPropertyNames()、for...in...对比
1//Non-enumerable Object Properties2var nonenum =Object.create ({}, {3getfoo: {4 value:function() {5ReturnThis. foo;6},7 enumerable:False8}9});Ten Nonenum.foo = 1;NONENUM.ASJ = 2;1213//Gets an enumerable or non-enumerable property of an objectConsole.log (object.getownpropertynames (nonenum). Sort ());//Output ["asj", "foo", "getfoo"]1516// Gets the object enumerable property 17 console.log (object.keys (nonenum). sort ()) ; // output ["asj", "foo"]18 Span style= "color: #008080" >19 // Returns an enumerable property that is directly defined on the object 20 for (var i in nonenum) {21 if  (nonenum.hasownproperty (i)) {22 console.log (i); // output foo Asj23 }24}          
third, Get The JavaScript object property name and method name, respectively
1//To create a method for constructing an object2functionMYOBJ (name, Attr) {3THIS.name =Name4This.attr =attr56This.sayhi =function() {7Return ' Hi everyone!!! ';8}9}1011//Create an Object12var mytester =New MYOBJ ("shinejaie", 1)1314//Get Object methods15for (var iInchMytester) {16If (mytester.hasownproperty (i) &&typeof mytester[i] = = "function") {Console.log ("object method:", i, "=", mytester[i])18}19}20//Output object Method: sayhi = () {return ' Hi everyone!!! ';}21 22 // Get Object Properties 23 for (in mytester) {24 if (mytester.hasownproperty (i) && typeof mytester[i]! = " function "" {25 console.log ("object properties:" , i); 26 }27 }28 // Output Object properties: Name Object properties: attr              

Go: JavaScript gets object properties and methods

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.