Merge pull request #9120 from Polyrhythm/ryan/bind-polyfill

add bind polyfill and basic .editorconfig for indentation
This commit is contained in:
Chris Collins 2016-11-30 09:52:56 -08:00 committed by GitHub
commit c2863f3413
3 changed files with 65 additions and 0 deletions

6
.editorconfig Normal file
View file

@ -0,0 +1,6 @@
root = true
# 4-space indentation
[*]
indent_style = space
indent_size = 4

View file

@ -0,0 +1,31 @@
Script.include('../libraries/jasmine/jasmine.js');
Script.include('../libraries/jasmine/hifi-boot.js');
Script.include('../../system/libraries/utils.js');
describe('Bind', function() {
it('functions should have bind available', function() {
var foo = 'bar';
function callAnotherFn(anotherFn) {
return anotherFn();
}
function TestConstructor() {
this.foo = foo;
}
TestConstructor.prototype.doSomething = function() {
return callAnotherFn(function() {
return this.foo;
}.bind(this));
};
var instance = new TestConstructor();
expect(typeof(instance.doSomething.bind) !== 'undefined');
expect(instance.doSomething()).toEqual(foo);
});
});
jasmine.getEnv().execute();
Script.stop();

View file

@ -6,6 +6,34 @@
// See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html
//
if (!Function.prototype.bind) {
Function.prototype.bind = function(oThis) {
if (typeof this !== 'function') {
// closest thing possible to the ECMAScript 5
// internal IsCallable function
throw new TypeError('Function.prototype.bind - what is trying to be bound is not callable');
}
var aArgs = Array.prototype.slice.call(arguments, 1),
fToBind = this,
fNOP = function() {},
fBound = function() {
return fToBind.apply(this instanceof fNOP
? this
: oThis,
aArgs.concat(Array.prototype.slice.call(arguments)));
};
if (this.prototype) {
// Function.prototype doesn't have a prototype property
fNOP.prototype = this.prototype;
}
fBound.prototype = new fNOP();
return fBound;
};
}
vec3toStr = function(v, digits) {
if (!digits) { digits = 3; }
return "{ " + v.x.toFixed(digits) + ", " + v.y.toFixed(digits) + ", " + v.z.toFixed(digits)+ " }";