What does the meaning mean?

I was redirected to MDN for .. on the page when he said: "for..in Iterates over the enumerated properties of the object."

Then I went to the Enumerability and ownership of properties page, where he said: "Enumerated properties are those that can be repeated using. In a loop."

The dictionary defines the enumerated as countable, but I can not really imagine what this means. Can I get an example of what can be listed?

+123
javascript enumerable
Jul 27 '13 at 2:38
source share
7 answers

An enumerated property is a property that can be included and visited during for..in (or a similar iteration of properties, such as Object.keys() ).

If the property is not identified as enumerable, the loop ignores it in the object.

 var obj = { key: 'val' }; console.log('toString' in obj); // true console.log(typeof obj.toString); // "function" for (var key in obj) console.log(key); // "key" 



A property is identified as enumerable or not by its own attribute [[Enumerable]] . You can view this as part of the property descriptor :

 var descriptor = Object.getOwnPropertyDescriptor({ bar: 1 }, 'bar'); console.log(descriptor.enumerable); // true console.log(descriptor.value); // 1 console.log(descriptor); // { value: 1, writable: true, enumerable: true, configurable: true } 

for..in loop for..in executes the object property names.

 var foo = { bar: 1, baz: 2}; for (var prop in foo) console.log(prop); // outputs 'bar' and 'baz' 

But, only evaluates his statement - console.log(prop); in this case, for those properties whose [[Enumerable]] attribute is true .

This condition is satisfied because objects have much more properties , especially from inheritance :

 console.log(Object.getOwnPropertyNames(Object.prototype)); // ["constructor", "toString", "toLocaleString", "valueOf", "hasOwnProperty", "isPrototypeOf", "propertyIsEnumerable", /* etc. */] 

Each of these properties still exists in the object :

 console.log('constructor' in foo); // true console.log('toString' in foo); // true // etc. 

But they are skipped for..in because they are not listed.

 var descriptor = Object.getOwnPropertyDescriptor(Object.prototype, 'constructor'); console.log(descriptor.enumerable); // false 
+122
Jul 27 '13 at 2:47 on
source share

If you create an object via myObj = {foo: 'bar'} or something like that, all properties are listed. So it’s easier to ask a question, which is not enumerable? Some objects have some non-enumerable properties, for example, if you call Object.getOwnPropertyNames([]) (which returns an array of all properties, enumerated or not, in []), it returns ['length'] , which includes the non-enumerable array property , 'length'.

You can make your own non-enumerable properties by calling Object.defineProperty :

 var person = { age: 18 }; Object.defineProperty(person, 'name', { value: 'Joshua', enumerable: false }); person.name; // 'Joshua' for (prop in person) { console.log(prop); }; // 'age' 

This example is heavily dependent on Enumerable properties in JavaScript , but shows an enumerated object. Properties may or may not be rewritable, custom, or enumerable. John Resig discusses this under the Objects and Properties of ECMAScript 5 .

And there is a question about the stack overflow about why you would ever want to make properties non-enumerable .

+42
Jul 27 '13 at 2:59
source share

This is much more boring than something you need to visualize.

There is literally an attribute for all properties called enumerated. When set to false, the for..in method for..in skip this property, pretend that it does not exist.

There are many properties of objects for which "enumerated" is set to false, for example, "valueOf" and "hasOwnProperty" because it suggested that you do not want the JavaScript engine to repeat them.

You can create your own non-enumerable properties using the Object.defineProperty method:

  var car = { make: 'Honda', model: 'Civic', year: '2008', condition: 'bad', mileage: 36000 }; Object.defineProperty(car, 'mySecretAboutTheCar', { value: 'cat pee in back seat', enumerable: false }); 

Now hiding the fact that there is even a secret about the car. Of course, they can access the property directly and get an answer:

 console.log(car.mySecretAboutTheCar); // prints 'cat pee in back seat' 

But they should know that the property exists in the first place, because if they try to access it through for..in or Object.keys , it will remain completely secret:

 console.log(Object.keys(car)); //prints ['make', 'model', 'year', 'condition', 'mileage'] 

They should just call it "forInAble".

+29
Nov 19 '15 at 7:21
source share

If you are having difficulty visualizing "what does it mean to be enumerated?" why not ask yourself what it means to be win-win?

I think this is a bit like an unbearable property exists , but partially hidden ; which means that the unrecognizable is strange. Now you can imagine the enumerable as what remains - a more natural property that we are used to encountering since we discovered objects. Consider

 var o = {}; o['foo'] = 0; // enumerable, normal Object.defineProperty(o, 'bar', {value: 1}); // nonenumerable, weird 

Now in for..in , imagine this is pseudo code

 for property in o: if not property enumerable continue // skip non-enumerable, "bar" else do /* whatever */ // act upon enumerable, "foo" 

where the loop body you entered in JavaScript is instead of /* whatever */

+9
Jul 27 '13 at 3:01
source share

I will write one line of the definition of ENUMERABLE

Enumerable : indicates whether a property can be returned in a for / in loop.

 var obj = {}; Object.defineProperties(data, { set1: {enumerable: true}, set2: {enumerable: false}, }); Object.keys(obj); // ["set1"] Object.getOwnPropertyNames(obj); // ["set1", "set2"] 
+3
Aug 02 '16 at 13:26
source share

methods are not enumerable; or rather, the built-in methods are not ... after searching for what the enumerated means java script; it simply refers to a property attribute .. all created objects in ecma3 are enumerable, and ecma5 u can now define it .... that it ..: D lol took me a little to find the answer; but I believe that it was told in David Flanagan’s book .. so I think it means “hidden” or not “hidden” in that the methods are not shown in the for in loop and thus are “hidden”

0
Dec 04 '15 at 10:11
source share

I suggest you guys keep an eye on this little raw code hosted in my own gitHub repository. (link below) https://raw.githubusercontent.com/projektorius96/Global.prototype/master/Object.defineProperty ()

Hope this helps.

0
Jan 25 '19 at 20:28
source share



All Articles