Comparing version 1.0.3 to 1.1.0
102
lib/proto.js
@@ -0,1 +1,3 @@ | ||
/* global define, exports, module, window */ | ||
/** | ||
@@ -8,44 +10,17 @@ * A base object for ECMAScript 5 style prototypal inheritance. | ||
*/ | ||
(function () | ||
{ | ||
if (typeof define == "undefined") { | ||
/** | ||
* AMD module shim | ||
* @param {Function} fn The callback function | ||
*/ | ||
define = function (fn) | ||
{ | ||
var res = fn(); | ||
if (typeof exports == "undefined") { | ||
Proto = res; | ||
} else { | ||
module.exports = res; | ||
} | ||
(function () { | ||
/** | ||
* AMD module shim | ||
* @param {Function} fn The callback function | ||
*/ | ||
var def = (typeof define === "undefined") ? function (fn) { | ||
var res = fn(); | ||
if (typeof exports === "undefined" && typeof window !== "undefined") { | ||
window.Proto = res; | ||
} else { | ||
module.exports = res; | ||
} | ||
}; | ||
} : define; | ||
if (!Object.create) { | ||
/** | ||
* Object.create polyfill | ||
* @param o The object to set the prototype to | ||
* @see https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Object/create | ||
*/ | ||
Object.create = function (o) { | ||
if (arguments.length > 1) { | ||
throw new Error('Object.create implementation only accepts the first parameter.'); | ||
} | ||
function F() {} | ||
F.prototype = o; | ||
return new F(); | ||
}; | ||
} | ||
if (!Object.getPrototypeOf) { | ||
Object.getPrototypeOf = function (object) { | ||
return object.proto || object.constructor.prototype; | ||
}; | ||
} | ||
define(function () | ||
{ | ||
def(function () { | ||
return { | ||
@@ -57,7 +32,6 @@ /** | ||
*/ | ||
create : function () | ||
{ | ||
create: function () { | ||
var instance = Object.create(this), | ||
init = typeof instance.__init === 'string' ? instance.__init : 'init'; | ||
if (typeof instance[init] == "function") { | ||
init = typeof instance.__init === 'string' ? instance.__init : 'init'; | ||
if (typeof instance[init] === "function") { | ||
instance[init].apply(instance, arguments); | ||
@@ -72,9 +46,5 @@ } | ||
*/ | ||
mixin : function (prop, obj) | ||
{ | ||
mixin: function (prop, obj) { | ||
var self = obj || this, | ||
fnTest = /xyz/.test(function () | ||
{ | ||
xyz; | ||
}) ? /\b_super\b/ : /.*/, | ||
fnTest = /xyz/.test(function () {}) ? /\b_super\b/ : /.*/, | ||
_super = Object.getPrototypeOf(self) || self.prototype, | ||
@@ -88,8 +58,6 @@ _old; | ||
// Check if we're overwriting an existing function | ||
self[name] = (typeof prop[name] == "function" && typeof _super[name] == "function" && fnTest.test(prop[name])) | ||
|| (typeof _old == "function" && typeof prop[name] == "function") ? // | ||
(function (old, name, fn) | ||
{ | ||
return function () | ||
{ | ||
self[name] = (typeof prop[name] === "function" && typeof _super[name] === "function" && fnTest.test(prop[name])) || | ||
(typeof _old === "function" && typeof prop[name] === "function") ? // | ||
(function (old, name, fn) { | ||
return function () { | ||
var tmp = this._super; | ||
@@ -100,3 +68,3 @@ | ||
// or to the overwritten method | ||
this._super = (typeof old == 'function') ? old : _super[name]; | ||
this._super = (typeof old === 'function') ? old : _super[name]; | ||
@@ -122,4 +90,3 @@ // The method only need to be bound temporarily, so we | ||
*/ | ||
extend : function (prop, obj) | ||
{ | ||
extend: function (prop, obj) { | ||
return this.mixin(prop, Object.create(obj || this)); | ||
@@ -129,12 +96,11 @@ }, | ||
* Return a callback function with this set to the current or a given context object. | ||
* @param name Name of the method to prox | ||
* @param context [optional] The object to use as the context | ||
* @param name Name of the method to proxy | ||
* @param args... [optional] Arguments to use for partial application | ||
*/ | ||
proxy : function (name, context) | ||
{ | ||
var self = context || this; | ||
return function () | ||
{ | ||
return self[name].apply(self, arguments); | ||
} | ||
proxy: function (name) { | ||
var fn = this[name], | ||
args = Array.prototype.slice.call(arguments, 1); | ||
args.unshift(this); | ||
return fn.bind.apply(fn, args); | ||
} | ||
@@ -141,0 +107,0 @@ }; |
{ | ||
"name": "uberproto", | ||
"description": "JavaScript object inheritance sugar: Easy extension, mixins, super methods, proxies", | ||
"version": "1.0.3", | ||
"homepage": "http://daffl.github.com/uberproto", | ||
"repository": { | ||
"type": "git", | ||
"url": "git://github.com/daffl/uberproto.git" | ||
}, | ||
"author": "David Luecke <daff@neyeon.com> (http://neyeon.com)", | ||
"main": "lib/proto", | ||
"directories": { | ||
"lib": "lib" | ||
}, | ||
"scripts": { | ||
"test": "nodeunit test" | ||
}, | ||
"engines": { | ||
"node": "*" | ||
} | ||
"name": "uberproto", | ||
"description": "JavaScript object inheritance sugar: Easy extension, mixins, super methods, proxies", | ||
"version": "1.1.0", | ||
"homepage": "http://daffl.github.com/uberproto", | ||
"repository": { | ||
"type": "git", | ||
"url": "git://github.com/daffl/uberproto.git" | ||
}, | ||
"author": "David Luecke <daff@neyeon.com> (http://neyeon.com)", | ||
"main": "lib/proto", | ||
"directories": { | ||
"lib": "lib" | ||
}, | ||
"scripts": { | ||
"test": "mocha test" | ||
}, | ||
"licenses": [ | ||
{ | ||
"type": "MIT", | ||
"url": "http://www.opensource.org/licenses/mit-license.php" | ||
} | ||
], | ||
"devDependencies": { | ||
"mocha": "~ 1.8.0", | ||
"grunt": "~ 0.4.0", | ||
"grunt-contrib-concat": "*", | ||
"grunt-contrib-uglify": "*", | ||
"grunt-contrib-jshint": "*" | ||
}, | ||
"engines": { | ||
"node": "*" | ||
} | ||
} |
@@ -1,3 +0,4 @@ | ||
(function(){"undefined"==typeof define&&(define=function(a){a=a();"undefined"==typeof exports?Proto=a:module.exports=a});Object.create||(Object.create=function(a){function b(){}if(1<arguments.length)throw Error("Object.create implementation only accepts the first parameter.");b.prototype=a;return new b});Object.getPrototypeOf||(Object.getPrototypeOf=function(a){return a.proto||a.constructor.prototype});define(function(){return{create:function(){var a=Object.create(this),b=typeof a.__init==="string"? | ||
a.__init:"init";typeof a[b]=="function"&&a[b].apply(a,arguments);return a},mixin:function(a,b){var d=b||this,g=/xyz/.test(function(){xyz})?/\b_super\b/:/.*/,f=Object.getPrototypeOf(d)||d.prototype,e,c;for(c in a){e=d[c];d[c]=typeof a[c]=="function"&&typeof f[c]=="function"&&g.test(a[c])||typeof e=="function"&&typeof a[c]=="function"?function(a,b,c){return function(){var d=this._super;this._super=typeof a=="function"?a:f[b];var e=c.apply(this,arguments);this._super=d;return e}}(e,c,a[c]):a[c]}return d}, | ||
extend:function(a,b){return this.mixin(a,Object.create(b||this))},proxy:function(a,b){var d=b||this;return function(){return d[a].apply(d,arguments)}}}})})(); | ||
/*! uberproto - v1.1.0 - 2013-03-21 | ||
* http://daffl.github.com/uberproto | ||
* Copyright (c) 2013 ; Licensed MIT */ | ||
(function(){Object.create=Object.create||function(t){function n(){}if(arguments.length>1)throw Error("Object.create implementation only accepts the first parameter.");return n.prototype=t,new n},Object.getPrototypeOf=Object.getPrototypeOf||function(t){return t.proto||t.constructor.prototype},Function.prototype.bind=Function.prototype.bind||function(t){if("function"!=typeof this)throw new TypeError("Function.prototype.bind - what is trying to be bound is not callable");var n=Array.prototype.slice.call(arguments,1),e=this,o=function(){},r=function(){return e.apply(this instanceof o&&t?this:t,n.concat(Array.prototype.slice.call(arguments)))};return o.prototype=this.prototype,r.prototype=new o,r}})(),function(){var t="undefined"==typeof define?function(t){var n=t();"undefined"==typeof exports&&"undefined"!=typeof window?window.Proto=n:module.exports=n}:define;t(function(){return{create:function(){var t=Object.create(this),n="string"==typeof t.__init?t.__init:"init";return"function"==typeof t[n]&&t[n].apply(t,arguments),t},mixin:function(t,n){var e,o=n||this,r=/xyz/.test(function(){})?/\b_super\b/:/.*/,i=Object.getPrototypeOf(o)||o.prototype;for(var p in t)e=o[p],o[p]="function"==typeof t[p]&&"function"==typeof i[p]&&r.test(t[p])||"function"==typeof e&&"function"==typeof t[p]?function(t,n,e){return function(){var o=this._super;this._super="function"==typeof t?t:i[n];var r=e.apply(this,arguments);return this._super=o,r}}(e,p,t[p]):t[p];return o},extend:function(t,n){return this.mixin(t,Object.create(n||this))},proxy:function(t){var n=this[t],e=Array.prototype.slice.call(arguments,1);return e.unshift(this),n.bind.apply(n,e)}}})}(); |
260
readme.md
@@ -22,3 +22,5 @@ # Uberproto | ||
in the browser. If no module loader is available, the global variable _Proto_ | ||
will be defined after you include the script. | ||
will be defined after you include the script. In the browser you have two options: | ||
The default build that includes EcmaScript 5 shims or, if you only support modern browsers or | ||
provide the shims already, without. | ||
@@ -29,12 +31,18 @@ ### Using AMD (e.g. RequireJS) | ||
define(['proto'], function(Proto) { | ||
// Source goes here | ||
}); | ||
```javascript | ||
define(['proto'], function(Proto) { | ||
// Source goes here | ||
}); | ||
``` | ||
### In the browser | ||
[Download proto.min.js](https://raw.github.com/daffl/uberproto/master/proto.min.js) | ||
(1Kb minified) and include it as a script: | ||
[Download proto.min.js](https://raw.github.com/daffl/uberproto/master/dist/proto.min.js) or | ||
the EcmaScript 5 version [proto.es5.min.js](https://raw.github.com/daffl/uberproto/master/dist/proto.es5.min.js). | ||
You can also `bower install uberproto` if you are using [Bower](http://twitter.github.com/bower/) | ||
as your package manager. Then simply include the file as a script: | ||
<script type="text/javascript" src="proto.min.js"></script> | ||
```html | ||
<script type="text/javascript" src="proto.min.js"></script> | ||
``` | ||
@@ -51,3 +59,5 @@ Now *Proto* is available as a global vairable. | ||
var Proto = require('uberproto'); | ||
```javascript | ||
var Proto = require('uberproto'); | ||
``` | ||
@@ -65,23 +75,27 @@ ## Creating objects | ||
var Person = Proto.extend({ | ||
init : function(name) { | ||
this.name = name; | ||
}, | ||
fullName : function() { | ||
return this.name; | ||
} | ||
}); | ||
```javascript | ||
var Person = Proto.extend({ | ||
init : function(name) { | ||
this.name = name; | ||
}, | ||
fullName : function() { | ||
return this.name; | ||
} | ||
}); | ||
``` | ||
You can also define a plain object and pass it to UberProto object methods: | ||
var PersonObject = { | ||
init : function(name) { | ||
this.name = name; | ||
}, | ||
```javascript | ||
var PersonObject = { | ||
init : function(name) { | ||
this.name = name; | ||
}, | ||
fullName : function() { | ||
return this.name; | ||
} | ||
}; | ||
fullName : function() { | ||
return this.name; | ||
} | ||
}; | ||
``` | ||
@@ -95,5 +109,7 @@ Play around with the examples in [this JSFiddle](http://jsfiddle.net/Daff/2GB8n/1/). | ||
var dave = Person.create('Dave'); | ||
console.log(dave.name); // -> 'Dave' | ||
console.log(dave.fullName()); // -> 'Dave' | ||
```javascript | ||
var dave = Person.create('Dave'); | ||
console.log(dave.name); // -> 'Dave' | ||
console.log(dave.fullName()); // -> 'Dave' | ||
``` | ||
@@ -103,14 +119,18 @@ If you are using *init* already for something else you can also set the *__init* property to the method name | ||
var MyPerson = Proto.extend({ | ||
__init : 'construct', | ||
```javascript | ||
var MyPerson = Proto.extend({ | ||
__init : 'construct', | ||
construct : function(name) { | ||
this.name = name; | ||
} | ||
}); | ||
construct : function(name) { | ||
this.name = name; | ||
} | ||
}); | ||
``` | ||
For calling the constructor on a plain object, call *create* on an UberProto object: | ||
var john = Proto.create.call(PersonObject, 'John'); | ||
console.log(john.fullName()); // -> 'John' | ||
```javascript | ||
var john = Proto.create.call(PersonObject, 'John'); | ||
console.log(john.fullName()); // -> 'John' | ||
``` | ||
@@ -125,34 +145,37 @@ Overwriting *create* is great if you want to customize the way new objects are being | ||
var BetterPerson = Person.extend({ | ||
init : function(name, lastname) { | ||
// If you want to pass all original arguments to the | ||
// _super method just use apply: | ||
// this._super.apply(this, arguments); | ||
this._super(name); | ||
this.lastname = lastname; | ||
}, | ||
fullName : function() { | ||
return this._super() + ' ' + this.lastname; | ||
} | ||
}); | ||
```javascript | ||
var BetterPerson = Person.extend({ | ||
init : function(name, lastname) { | ||
// If you want to pass all original arguments to the | ||
// _super method just use apply: | ||
// this._super.apply(this, arguments); | ||
this._super(name); | ||
this.lastname = lastname; | ||
}, | ||
fullName : function() { | ||
return this._super() + ' ' + this.lastname; | ||
} | ||
}); | ||
var dave = BetterPerson.create('Dave', 'Doe'); | ||
console.log(dave.name); // -> 'Dave' | ||
console.log(dave.lastname); // -> 'Doe' | ||
console.log(dave.fullName()); // -> 'Dave Doe' | ||
var dave = BetterPerson.create('Dave', 'Doe'); | ||
console.log(dave.name); // -> 'Dave' | ||
console.log(dave.lastname); // -> 'Doe' | ||
console.log(dave.fullName()); // -> 'Dave Doe' | ||
``` | ||
You can also extend a plain object if you don't want to inherit from an UberProto object: | ||
var BetterPersonObject = Proto.extend({ | ||
init : function(name, lastname) { | ||
this._super(name); | ||
this.lastname = lastname; | ||
}, | ||
```javascript | ||
var BetterPersonObject = Proto.extend({ | ||
init : function(name, lastname) { | ||
this._super(name); | ||
this.lastname = lastname; | ||
}, | ||
fullName : function() { | ||
return this._super() + ' ' + this.lastname; | ||
} | ||
}, PersonObject); // Pass the plain object as the second parameter | ||
fullName : function() { | ||
return this._super() + ' ' + this.lastname; | ||
} | ||
}, PersonObject); // Pass the plain object as the second parameter | ||
``` | ||
@@ -164,43 +187,49 @@ ### Mixins | ||
Person.mixin({ | ||
init : function() | ||
{ | ||
this._super.apply(this, arguments); | ||
this.can_sing = true; | ||
}, | ||
sing : function() | ||
{ | ||
return 'Laaaa'; | ||
} | ||
}); | ||
```javascript | ||
Person.mixin({ | ||
init : function() | ||
{ | ||
this._super.apply(this, arguments); | ||
this.can_sing = true; | ||
}, | ||
var dude = Person.create('Dude'); | ||
console.log(dude.sing()); // -> 'Laaaa' | ||
console.log(dude.can_sing); // -> true | ||
sing : function() | ||
{ | ||
return 'Laaaa'; | ||
} | ||
}); | ||
var dude = Person.create('Dude'); | ||
console.log(dude.sing()); // -> 'Laaaa' | ||
console.log(dude.can_sing); // -> true | ||
``` | ||
Actual instances can be mixed in just the same: | ||
var operaSinger = Person.create('Pavarotti'); | ||
operaSinger.mixin({ | ||
sing : function() | ||
{ | ||
return this._super() + ' Laalaaa!'; | ||
} | ||
}); | ||
```javascript | ||
var operaSinger = Person.create('Pavarotti'); | ||
operaSinger.mixin({ | ||
sing : function() | ||
{ | ||
return this._super() + ' Laalaaa!'; | ||
} | ||
}); | ||
console.log(operaSinger.sing()); // -> 'Laaaa Laalaaa! | ||
console.log(operaSinger.sing()); // -> 'Laaaa Laalaaa!' | ||
``` | ||
And you can also mix into plain objects e.g. overwriting the constructor of PersonObject: | ||
Proto.mixin({ | ||
fullName : function() { | ||
return 'My name is: ' + this._super(); | ||
} | ||
}, PersonObject); | ||
```javascript | ||
Proto.mixin({ | ||
fullName : function() { | ||
return 'My name is: ' + this._super(); | ||
} | ||
}, PersonObject); | ||
// Create a plain object without calling the constructor | ||
var instance = Object.create(PersonObject); | ||
instance.name = 'Dude'; | ||
console.log(instance.fullName()); // 'My name is: Dude' | ||
// Create a plain object without calling the constructor | ||
var instance = Object.create(PersonObject); | ||
instance.name = 'Dude'; | ||
console.log(instance.fullName()); // 'My name is: Dude' | ||
``` | ||
@@ -211,12 +240,41 @@ ### Method proxy | ||
point to the right object: | ||
var callback = operaSinger.proxy('fullName'); | ||
console.log(callback()); // -> 'Pavarotti' | ||
And of course proxy methods of plain objects: | ||
```javascript | ||
var callback = operaSinger.proxy('fullName'); | ||
console.log(callback()); // -> 'Pavarotti' | ||
``` | ||
var cb = Proto.proxy('fullName', PersonObject); | ||
And you can partially apply function arguments: | ||
```javascript | ||
operaSinger.mixin({ | ||
sing : function(text) | ||
{ | ||
return this._super() + ' ' + text; | ||
} | ||
}); | ||
var singHello = operaSinger.proxy('sing', 'Helloooooo!'); | ||
singHello() // Laaaa Laalaaa! Helloooooo! | ||
``` | ||
`proxy` only works on objects extended from UberProto. | ||
## Changelog | ||
__1.1.0__ | ||
* Extract ES5 shims (build shim-less version) | ||
* Use Function.bind for proxy | ||
* Switched test suite to [Mocha](http://visionmedia.github.com/mocha/) | ||
* [GruntJS](http://gruntjs.com/) build | ||
* [Bower](http://twitter.github.com/bower/) component: `bower install uberproto` | ||
__1.0.3__ | ||
* Added `Object.getPrototypeOf` shim | ||
* Updated documentation | ||
* Added Travis CI | ||
__1.0.2__ | ||
@@ -236,3 +294,3 @@ | ||
Copyright (C) 2012 David Luecke daff@neyeon.com | ||
Copyright (C) 2013 David Luecke daff@neyeon.com | ||
@@ -239,0 +297,0 @@ Permission is hereby granted, free of charge, to any person obtaining a copy |
303
test/test.js
@@ -1,188 +0,167 @@ | ||
if(typeof Proto == "undefined") { | ||
var Proto = require('../lib/proto'); | ||
} | ||
/* global describe, it, window, require */ | ||
(function () { | ||
var Proto, test; | ||
this.suite = { | ||
extend : function(test) { | ||
test.expect(2); | ||
var Extended = Proto.extend({ | ||
sayHi : function() { | ||
test.ok(true, 'sayHi called'); | ||
return 'hi'; | ||
} | ||
if (typeof require === 'function') { | ||
Proto = require('../lib/proto'); | ||
test = require('assert'); | ||
} else { | ||
Proto = window.Proto; | ||
test = window.assert; | ||
} | ||
describe('UberProto', function () { | ||
it('extends objects', function () { | ||
var Extended = Proto.extend({ | ||
sayHi: function () { | ||
test.ok(true, 'sayHi called'); | ||
return 'hi'; | ||
} | ||
}); | ||
test.equal(Extended.create().sayHi(), "hi", "Said hi"); | ||
}); | ||
test.equal(Extended.create().sayHi(), "hi", "Said hi"); | ||
test.done(); | ||
}, | ||
create : function(test) { | ||
test.expect(5); | ||
var Obj = Proto.extend({ | ||
init : function(name) | ||
{ | ||
test.ok(true, 'Init called'); | ||
this.name = name; | ||
}, | ||
sayHi : function() | ||
{ | ||
return 'Hi ' + this.name; | ||
} | ||
it('creates a new object', function () { | ||
var Obj = Proto.extend({ | ||
init: function (name) { | ||
test.ok(true, 'Init called'); | ||
this.name = name; | ||
}, | ||
sayHi: function () { | ||
return 'Hi ' + this.name; | ||
}, | ||
prop: 'Testing' | ||
}); | ||
var inst = Obj.create('Tester'); | ||
test.equal(inst.name, 'Tester', 'Name set'); | ||
test.equal(inst.prop, 'Testing', 'Prototype property still there'); | ||
test.equal(inst.sayHi(), 'Hi Tester', 'Said hi with name'); | ||
test.ok(Proto.isPrototypeOf(Obj), 'Should have prototype of Proto'); | ||
test.ok(Obj.isPrototypeOf(inst), 'Instance should have prototype of Obj'); | ||
}); | ||
var inst = Obj.create('Tester'); | ||
test.equal(inst.name, 'Tester', 'Name set'); | ||
test.equal(inst.sayHi(), 'Hi Tester', 'Said hi with name'); | ||
test.ok(Proto.isPrototypeOf(Obj), 'Should have prototype of Proto'); | ||
test.ok(Obj.isPrototypeOf(inst), 'Instance should have prototype of Obj'); | ||
test.done(); | ||
}, | ||
initAlias : function(test) { | ||
test.expect(2); | ||
var Obj = Proto.extend({ | ||
__init : 'myConstructor', | ||
myConstructor : function(arg) { | ||
test.equal(arg, 'myConstructor', 'Got proper arguments in myConstructor'); | ||
it('uses an init method alias', function () { | ||
var Obj = Proto.extend({ | ||
__init: 'myConstructor', | ||
myConstructor: function (arg) { | ||
test.equal(arg, 'myConstructor', 'Got proper arguments in myConstructor'); | ||
} | ||
}), | ||
OtherObj = { | ||
__init: 'testConstructor', | ||
testConstructor: function (arg) { | ||
test.equal(arg, 'testConstructor', 'Got proper arguments in myConstructor'); | ||
} | ||
}; | ||
Obj.create('myConstructor'); | ||
Proto.create.call(OtherObj, 'testConstructor'); | ||
}); | ||
it('uses _super', function () { | ||
var Obj = Proto.extend({ | ||
init: function (name) { | ||
test.ok(true, 'Super init called'); | ||
this.name = name; | ||
} | ||
}), | ||
OtherObj = { | ||
__init : 'testConstructor', | ||
testConstructor : function(arg) { | ||
test.equal(arg, 'testConstructor', 'Got proper arguments in myConstructor'); | ||
}), Sub = Obj.extend({ | ||
init: function () { | ||
this._super.apply(this, arguments); | ||
test.ok(true, 'Sub init called'); | ||
} | ||
} | ||
}); | ||
Obj.create('myConstructor'); | ||
Proto.create.call(OtherObj, 'testConstructor'); | ||
test.done(); | ||
}, | ||
_super : function(test) { | ||
test.expect(3); | ||
var Obj = Proto.extend({ | ||
init : function(name) | ||
{ | ||
test.ok(true, 'Super init called'); | ||
this.name = name; | ||
} | ||
}), Sub = Obj.extend({ | ||
init : function() { | ||
this._super.apply(this, arguments); | ||
test.ok(true, 'Sub init called'); | ||
} | ||
var inst = Sub.create('Tester'); | ||
test.equal(inst.name, 'Tester', 'Name set in prototype'); | ||
}); | ||
var inst = Sub.create('Tester'); | ||
test.equal(inst.name, 'Tester', 'Name set in prototype'); | ||
test.done(); | ||
}, | ||
it('extends an existing object', function () { | ||
var Obj = { | ||
test: function (name) { | ||
test.ok(true, 'Super test method called'); | ||
this.name = name; | ||
} | ||
}; | ||
extendObject : function(test) | ||
{ | ||
test.expect(3); | ||
var Extended = Proto.extend({ | ||
test: function () { | ||
this._super.apply(this, arguments); | ||
test.ok(true, 'Sub init called'); | ||
} | ||
}, Obj); | ||
var Obj = { | ||
test : function(name) | ||
{ | ||
test.ok(true, 'Super test method called'); | ||
this.name = name; | ||
} | ||
}; | ||
Extended.test('Tester'); | ||
var Extended = Proto.extend({ | ||
test : function() { | ||
this._super.apply(this, arguments); | ||
test.ok(true, 'Sub init called'); | ||
} | ||
}, Obj); | ||
test.equal(Extended.name, 'Tester', 'Name set in prototype'); | ||
}); | ||
Extended.test('Tester'); | ||
it('uses .mixin', function () { | ||
var Obj = Proto.extend({ | ||
init: function (name) { | ||
test.ok(true, 'Init called'); | ||
this.name = name; | ||
} | ||
}); | ||
test.equal(Extended.name, 'Tester', 'Name set in prototype'); | ||
Obj.mixin({ | ||
test: function () { | ||
return this.name; | ||
} | ||
}); | ||
test.done(); | ||
}, | ||
var inst = Obj.create('Tester'); | ||
test.equal(inst.test(), 'Tester', 'Mixin returned name'); | ||
mixin : function(test) { | ||
test.expect(3); | ||
var Obj = Proto.extend({ | ||
init : function(name) | ||
{ | ||
test.ok(true, 'Init called'); | ||
this.name = name; | ||
} | ||
Obj.mixin({ | ||
test: function () { | ||
return this._super() + ' mixed in'; | ||
} | ||
}); | ||
test.equal(inst.test(), 'Tester mixed in', 'Mixin called overwritten'); | ||
}); | ||
Obj.mixin({ | ||
test : function() | ||
{ | ||
return this.name; | ||
} | ||
}); | ||
var inst = Obj.create('Tester'); | ||
test.equal(inst.test(), 'Tester', 'Mixin returned name'); | ||
Obj.mixin({ | ||
test : function() { | ||
return this._super() + ' mixed in'; | ||
} | ||
}); | ||
test.equal(inst.test(), 'Tester mixed in', 'Mixin called overwritten'); | ||
test.done(); | ||
}, | ||
mixinObject : function(test) | ||
{ | ||
test.expect(3); | ||
it('.mixin(Object)', function () { | ||
var Obj = { | ||
test: function (name) { | ||
test.ok(true, 'Super test method called'); | ||
this.name = name; | ||
} | ||
}; | ||
var Obj = { | ||
test : function(name) | ||
{ | ||
test.ok(true, 'Super test method called'); | ||
this.name = name; | ||
} | ||
}; | ||
Proto.mixin({ | ||
test: function () { | ||
this._super.apply(this, arguments); | ||
test.ok(true, 'Sub init called'); | ||
} | ||
}, Obj); | ||
Proto.mixin({ | ||
test : function() { | ||
this._super.apply(this, arguments); | ||
test.ok(true, 'Sub init called'); | ||
} | ||
}, Obj); | ||
Obj.test('Tester'); | ||
Obj.test('Tester'); | ||
test.equal(Obj.name, 'Tester', 'Name set in prototype'); | ||
}); | ||
test.equal(Obj.name, 'Tester', 'Name set in prototype'); | ||
it('uses .proxy', function () { | ||
var Obj = Proto.extend({ | ||
init: function (name) { | ||
this.name = name; | ||
}, | ||
test.done(); | ||
}, | ||
test: function (arg) { | ||
return this.name + ' ' + arg; | ||
} | ||
}); | ||
proxy : function(test) { | ||
test.expect(1); | ||
var Obj = Proto.extend({ | ||
init : function(name) | ||
{ | ||
this.name = name; | ||
}, | ||
test : function(arg) { | ||
return this.name + ' ' + arg; | ||
} | ||
var inst = Obj.create('Tester'), | ||
callback = inst.proxy('test'); | ||
test.equal(callback('arg'), 'Tester arg', 'Callback set scope properly'); | ||
callback = inst.proxy('test', 'partialed'); | ||
test.equal(callback(), 'Tester partialed', 'Callback partially applied'); | ||
}); | ||
var inst = Obj.create('Tester'), | ||
callback = inst.proxy('test'); | ||
test.equal(callback('arg'), 'Tester arg', 'Callback set scope properly'); | ||
test.done(); | ||
} | ||
}; | ||
}); | ||
})(); |
Sorry, the diff of this file is not supported yet
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
306
32230
5
15
578
1