2014-04-02 06:25:51 -04:00
|
|
|
/**
|
|
|
|
* @fileOverview inherit, an alternative for __proto__
|
|
|
|
* @author YUKI "Piro" Hiroshi
|
2014-04-04 14:15:24 -04:00
|
|
|
* @contributor Infocatcher
|
|
|
|
* @version 3
|
2014-04-02 06:25:51 -04:00
|
|
|
*
|
|
|
|
* @license
|
|
|
|
* The MIT License, Copyright (c) 2014 YUKI "Piro" Hiroshi.
|
|
|
|
* https://github.com/piroor/fxaddonlib-inherit/blob/master/LICENSE
|
|
|
|
* @url http://github.com/piroor/fxaddonlib-inherit
|
|
|
|
*/
|
|
|
|
|
2015-10-10 23:40:17 -04:00
|
|
|
var EXPORTED_SYMBOLS = ['inherit'];
|
2014-04-02 06:25:51 -04:00
|
|
|
|
|
|
|
function toPropertyDescriptors(aProperties) {
|
|
|
|
var descriptors = {};
|
|
|
|
Object.keys(aProperties).forEach(function(aProperty) {
|
|
|
|
var description = Object.getOwnPropertyDescriptor(aProperties, aProperty);
|
|
|
|
descriptors[aProperty] = description;
|
|
|
|
});
|
|
|
|
return descriptors;
|
|
|
|
}
|
|
|
|
|
|
|
|
function inherit(aParent, aExtraProperties) {
|
2014-04-04 14:15:24 -04:00
|
|
|
var global;
|
|
|
|
if (Components.utils.getGlobalForObject)
|
|
|
|
global = Components.utils.getGlobalForObject(aParent);
|
|
|
|
else
|
|
|
|
global = aParent.valueOf.call();
|
|
|
|
global = global || this;
|
|
|
|
|
|
|
|
var ObjectClass = global.Object || Object;
|
|
|
|
|
|
|
|
if (!ObjectClass.create) {
|
|
|
|
aExtraProperties = aExtraProperties || new ObjectClass;
|
2014-04-02 06:25:51 -04:00
|
|
|
aExtraProperties.__proto__ = aParent;
|
|
|
|
return aExtraProperties;
|
|
|
|
}
|
|
|
|
if (aExtraProperties)
|
2014-04-04 14:15:24 -04:00
|
|
|
return ObjectClass.create(aParent, toPropertyDescriptors(aExtraProperties));
|
2014-04-02 06:25:51 -04:00
|
|
|
else
|
2014-04-04 14:15:24 -04:00
|
|
|
return ObjectClass.create(aParent);
|
2014-04-02 06:25:51 -04:00
|
|
|
}
|