Skip to content
This repository has been archived by the owner on Apr 26, 2024. It is now read-only.

Commit

Permalink
Added support for Object.prototype.__proto__ in unsupported browsers …
Browse files Browse the repository at this point in the history
…using Object.defineProperty and Object.getPrototypeOf
  • Loading branch information
constantology committed Dec 11, 2012
1 parent b0c9611 commit 428a7bb
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 139 deletions.
11 changes: 9 additions & 2 deletions README.md
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -938,15 +938,22 @@ Returns the `values` of the passed Object based on it's enumerable keys.


``` ```


### GET: Object.prototype.\_\_proto\_\_:String
Some browsers — like MSIE 9 & 10 which `m8` supports — do not support the non-standard property `__proto__`.

Luckily however, they do support `Object.getPrototypeOf`, which will return the same value as `__proto__`.

`m8` conveniently wraps this call up inside the `__proto__` getter for those browsers, so you can (more) easily work with `Object` prototypes.

### GET: Object.prototype.\_\_type\_\_:String ### GET: Object.prototype.\_\_type\_\_:String
Attempts to resolve a normalised type for any type that inherits from JavaScript's `Object.prototype`. See `m8.type` for more information. Attempts to resolve a normalised type for any type that inherits from JavaScript's `Object.prototype`. See `m8.type` for more information.


**NOTE:** All types are **always** in lowercase **NOTE:** All types are **always** in lowercase


## File size ## File size


- m8.js ≅ 6.2kb (gzipped) - m8.js ≅ 6.4kb (gzipped)
- m8.min.js ≅ 3.5kb (minzipped) - m8.min.js ≅ 3.6kb (minzipped)


## License ## License


Expand Down
135 changes: 0 additions & 135 deletions m8.js
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -387,141 +387,6 @@
Type[__xid__] = extenders.length; // assigned every time __lib__.x() is called, and Type[__xid__] = extenders.length; // assigned every time __lib__.x() is called, and
} // potentilly throwing overwrite errors. } // potentilly throwing overwrite errors.


/*~ src/nativex.js ~*/
x.cache( 'Array', function( Type ) {
var PROTO = Type.prototype;

def( Type, 'coerce', function( a, i, j ) {
if ( !got( a, 'length' ) ) return [a];
i = type( i ) == 'number' ? i > 0 ? i : 0 : 0;
j = type( j ) == 'number' ? j > i ? j : j <= 0 ? a.length + j : i + j : a.length;
return slice.call( a, i, j );
}, 'w' );

defs( PROTO, {
find : function( fn, ctx ) {
var i = -1, l = this.length >>> 0;
ctx || ( ctx = this );
while ( ++i < l ) if ( !!fn.call( ctx, this[i], i, this ) ) return this[i];
return null;
},
invoke : function( fn ) {
var args = Type.coerce( arguments, 1 );
return PROTO.map.call( this, function( item ) {
return item && typeof item[fn] == 'function' ? item[fn].apply( item, args ) : UNDEF;
} );
},
pluck : function( key, existing_only ) {
existing_only = existing_only === true;
return PROTO.reduce.call( this, function( val, item ) {
var v = Object.value( item, key );

( existing_only && !exists( v ) ) || val.push( v );

return val;
}, [] );
}
}, 'w' );
} );

x.cache( 'Boolean', function( Type ) {
def( Type, 'coerce', function( item ) {
switch( type( item ) ) {
case 'boolean' : return item;
case 'nan' : case false : return false;
case 'number' : case 'string' : return !( item in force ? !force[item] : Number( item ) === 0 );
}
return true;
}, 'w' );
} );

x.cache( 'Function', function( Type ) {
function anon( name ) { return !name || name in anon_list; }
function toString() { return this.toString(); }
function valueOf() { return this; }

var __xname__ = '__xname__',
anon_list = { Anonymous : true, anonymous : true },
desc = { mimic : function( fn, name ) {
var fn_val = fn.valueOf(); // in case fn is a mimicked Function, we'll want to mimic the original
defs( this, {
displayName : ( name || fname( fn_val ) ),
toString : toString.bind( fn_val ),
valueOf : valueOf.bind( fn_val )
}, 'c', true );
return this;
} };
desc[__name__] = { get : function() {
if ( !this[__xname__] ) {
var fn = this.valueOf(), // if this function is mimicking another, get the mimicked function
// handles anonymous functions which are mimicking (see mimic below) named functions
name_m = fn !== this ? !anon( fn[__name__] ) ? fn[__name__] : null : null,
name = name_m || fname( this );
!anon( name ) || anon( this.displayName ) || ( name = this.displayName );
def( this, __xname__, ( name || 'anonymous' ), 'w' );
}
return this[__xname__];
} };

defs( Type.prototype, desc, 'w' );
// allows us to better try and get a functions name, you can add to this list if you like
def( Type, 'anon_list', { value : anon_list }, 'w' );

} );

x.cache( 'Object', function( Type ) {
// this is a special case which does not use __lib__.describe
// since it internally uses __type__ which is about to be set up here.
def( Type.prototype, __type__, copy( { get : function() {
var _type_, item = this, ctor = item.constructor, ntype = nativeType( item ),
dtype = domType( ntype ) || ( re_global.test( ntype ) ? 'global' : false );

if ( dtype ) return dtype;
if ( ntype == 'number' ) return isNaN( item ) ? 'nan' : 'number';

if ( ntype == 'object' && typeof ctor == 'function' ) {
if ( ctor[__type__] != 'function' ) {
_type_ = String( ctor[__name__] ).toLowerCase();
return !_type_ || _type_ == 'anonymous' ? ctor[__type__] || ntype : _type_;
}
}

return ntype;
} }, modes.r ) );

defs( Type, {
key : function( item, val ) {
return Type.keys( Type( item ) ).find( function( key ) {
return item[key] === val;
} );
},
reduce : function( item, fn, val ) {
return Type.keys( Type( item ) ).reduce( function( res, key, i ) {
res = fn.call( item, res, item[key], key, item, i );
return res;
}, val );
},
value : function( item, key ) {
if ( isNaN( key ) ) {
if ( got( item, key ) ) return item[key];
if ( !!~key.indexOf( '.' ) ) {
var val; key = key.split( '.' );
while ( val = key.shift() )
if ( ( item = Type.value( item, val ) ) === UNDEF )
break;
return item;
}
}
return empty( item )
? UNDEF : exists( item[key] )
? item[key] : typeof item.get == 'function'
? item.get( key ) : typeof item.getAttribute == 'function'
? item.getAttribute( key ) : UNDEF;
},
values : function( item ) { return Type.keys( Object( item ) ).map( function( key ) { return item[key]; } ); }
}, 'w' );
} );

/*~ src/expose.js ~*/ /*~ src/expose.js ~*/
iter( PACKAGE ) || ( PACKAGE = ENV == 'commonjs' ? module : root ); iter( PACKAGE ) || ( PACKAGE = ENV == 'commonjs' ? module : root );


Expand Down
Loading

0 comments on commit 428a7bb

Please sign in to comment.