-
Notifications
You must be signed in to change notification settings - Fork 2k
Commit
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,61 +3,7 @@ | |
* Use of this source code is governed by a BSD-style | ||
* license that can be found in the LICENSE file. | ||
*/ | ||
(function() { | ||
// $class | ||
|
||
function $class(inExtends, inProperties) { | ||
// support optional inExtends (rare exception: allow two signatures) | ||
if (arguments.length == 1) { | ||
inProperties = inExtends; | ||
inExtends = null; | ||
} | ||
// make sure we have a `constructor` property one way or another | ||
// it's important to name the method for `super` to work | ||
// the default constructor calls `super` | ||
if (!inProperties || !inProperties.hasOwnProperty('constructor')) { | ||
inProperties.constructor = function() { | ||
this.super(); | ||
} | ||
} | ||
// use the supplied constructor or a stock version | ||
var ctor = inProperties.constructor; | ||
// base prototype is either supplied or stock | ||
var basePrototype = inExtends && inExtends.prototype | ||
|| Object.prototype; | ||
// use `extend` primitive to build new prototype | ||
ctor.prototype = extend(basePrototype, inProperties); | ||
// install `super` functionality if needed | ||
if (!('super' in ctor.prototype)) { | ||
ctor.prototype.super = $super; | ||
} | ||
// return newly minted constructor | ||
return ctor; | ||
}; | ||
|
||
// extend | ||
|
||
// return a prototype containing inProperties chained to inBasePrototype | ||
function extend(inBasePrototype, inProperties) { | ||
return Object.create(inBasePrototype, | ||
getPropertyDescriptors(inProperties)); | ||
} | ||
|
||
// copy property inName from inSource object to inTarget object | ||
function getPropertyDescriptors(inObject) { | ||
var descriptors = {}; | ||
for (var n in inObject) { | ||
descriptors[n] = getPropertyDescriptor(inObject, n); | ||
} | ||
return descriptors; | ||
} | ||
|
||
function getPropertyDescriptor(inObject, inName) { | ||
return inObject && | ||
Object.getOwnPropertyDescriptor(inObject, inName) || | ||
getPropertyDescriptor(Object.getPrototypeOf(inObject), inName); | ||
} | ||
|
||
(function(scope) { | ||
// super | ||
|
||
// TODO(sjmiles): | ||
|
@@ -78,14 +24,12 @@ | |
// find the caller (cannot be `strict` because of 'caller') | ||
var caller = $super.caller; | ||
// memoization for 'name of method' | ||
var nom = caller._nom; | ||
var nom = caller.nom; | ||
This comment has been minimized.
Sorry, something went wrong.
This comment has been minimized.
Sorry, something went wrong.
sjmiles
Contributor
|
||
if (!nom) { | ||
// once per call chain | ||
nom = caller._nom = nameInThis.call(this, caller); | ||
if (!nom) { | ||
console.warn('called super() on a method not in "this"'); | ||
return; | ||
} | ||
nom = nameInThis.call(this, caller); | ||
This comment has been minimized.
Sorry, something went wrong.
sorvell
Contributor
|
||
} | ||
if (!nom) { | ||
console.warn('called super() on a method not installed declaratively (has no .nom property)'); | ||
} | ||
// super prototype is either cached or we have to find it | ||
// by searching __proto__ (at the 'top') | ||
|
@@ -133,20 +77,24 @@ | |
}; | ||
|
||
function nameInThis(inValue) { | ||
for (var n in this) { | ||
var d = getPropertyDescriptor(this, n); | ||
if (d.value == inValue) { | ||
return n; | ||
console.group('nameInThis'); | ||
This comment has been minimized.
Sorry, something went wrong. |
||
var p = this; | ||
while (p && p !== HTMLElement.prototype) { | ||
var n$ = Object.getOwnPropertyNames(p); | ||
for (var i=0, l=n$.length, n; i<l && (n=n$[i]); i++) { | ||
console.log(n); | ||
var d = Object.getOwnPropertyDescriptor(p, n); | ||
if (d.value == inValue) { | ||
return n; | ||
} | ||
} | ||
p = Object.getPrototypeOf(p); | ||
} | ||
console.groupEnd('nameInThis'); | ||
} | ||
|
||
// exports | ||
|
||
// `class` is a reserved word | ||
window.$class = $class; | ||
window.extend = extend; | ||
// `super` is a reserved word | ||
window.$super = $super; | ||
scope.$super = $super; | ||
|
||
})(); | ||
})(Polymer); |
Note, at https://github.com/Polymer/polymer/blob/master/src/oop.js#L75 ._nom is set. Perhaps we can just remove the clause at https://github.com/Polymer/polymer/blob/master/src/oop.js#L72.