Snail
Snail

Reputation: 111

how do javascript get property value

Through the following code I can get a plugin object in chrome browser:

nav = window.navigator;
detectPlugin = function(pluginName, mimeType) {

    return nav.plugins && nav.plugins[pluginName] && nav.mimeTypes &&   nav.mimeTypes[mimeType] && nav.mimeTypes[mimeType].enabledPlugin ? nav.plugins[pluginName] : false;
};
obj = detectPlugin('Shockwave Flash', 'application/x-shockwave-flash');

And I can see the obj's properties through

Object.keys(obj)

which is

["0", "1", "length", "description", "filename", "name"]

or I can see these through chrome Console:

Plugin {0: MimeType, 1: MimeType, length: 2, description: "Shockwave Flash 13.0 r0", filename: "libpepflashplayer.so", name: "Shockwave Flash", item: function…}

And here is something that I don't understand, if I input

obj['application/x-shockwave-flash']

I get this

MimeType {enabledPlugin: Plugin, description: "Shockwave Flash", suffixes: "swf", type: "application/x-shockwave-flash"}

I know obj[0] is the property of 'MimeType', but I don't know why "obj['application/x-shockwave-flash']" can get this.

Any help would be great!

Upvotes: 1

Views: 272

Answers (1)

Paul S.
Paul S.

Reputation: 66364

Object.keys only returns

a given object's own enumerable properties

i.e. if it's inherited or non-enumberable, you won't see it


What does it mean for a property to be enumerable?

In JavaScript, you can think of it as similar to meaning "the property is visible to loops"

What does it mean for a property to be inherited?

An Object inherits properties and methods from it's prototype, this means you can have very generic Objects, e.g. {} instanceof Object; // true and very complicated ones like d = new Date(), where d instanceof Date; // true but it still has everything the from the more generic Object, i.e. d instanceof Object; // true


Consider this (assuming IE 9+)

var o = {};
Object.defineProperty(o, 'foo', {value:"bar", enumerable:false});
Object.keys(o); // []
o.foo; // "bar"

And

navigator.plugins[0].hasOwnProperty('application/x-shockwave-flash'); // true

Object.getOwnPropertyDescriptor(
    navigator.plugins[0],
    'application/x-shockwave-flash'
);
//Object {value: MimeType, writable: true, enumerable: false, configurable: true}

The MDN page here may be helpful in understanding enumerability and ownership

Upvotes: 1

Related Questions