Reputation: 135
mdn Object.keys() defines it will return an array of a given object's own enumerable properties.
var USComicPublishers = {
countryOfOrigin : {
value : "USA",
enumerable: false,
writable: false,
configurable: false
},
medium : {
value : "comic books",
enumerable: true,
writable: false,
configurable: false
}
}
as the Object.keys() define, it should return ['medium']
but it return ['countryOfOrigin', 'medium']
then I use Object.create()
to wrap it, it will return ['medium']
var us2 = Object.create({}, USComicPublishers)
console.log(us2)
// return ['medium']
does Object literal
has different behavior in Object.create()
?
var USComicPublishers = {
countryOfOrigin : {
value : "USA",
enumerable: false,
writable: false,
configurable: false
},
medium : {
value : "comic books",
enumerable: true,
writable: false,
configurable: false
}
}
console.log(Object.keys(USComicPublishers))
var us2 = Object.create({}, USComicPublishers)
console.log(Object.keys(us2))
console.log(Object.getOwnPropertyNames(us2))
Upvotes: 2
Views: 47
Reputation: 4736
Yes, there is a difference. When creating an object literal, it's not possible to set the enumerable
, writable
and configurable
properties like that. Instead, you're setting properties with all those values set to true
and value
set to the value you're giving (you'll notice in your first example that USComicPublishers.medium
will be an object and USComicPublishers.medium.value === "comic books"
)
You would have to create the object literal and then apply the properties using Object.defineProperties
if you wanted the same results.
var USComicPublishersData = {
countryOfOrigin : {
value : "USA",
enumerable: false,
writable: false,
configurable: false
},
medium : {
value : "comic books",
enumerable: true,
writable: false,
configurable: false
}
}
var USComicPublishers = {};
Object.defineProperties(USComicPublishers, USComicPublishersData);
console.log(Object.keys(USComicPublishers))
Upvotes: 3
Reputation: 943556
Given an object literal — { foo: bar }
— bar
is the value the property holds. It is the plain and simple value. It doesn't determine anything about the properly (such as if it is enumerable).
When passed to Object.create
, the value is taken as a description of the property. This determines if the property is enumerable etc.
Upvotes: 3