Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix Function.prototype.apply to work with generic array-like object instead of an array #100

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 44 additions & 0 deletions es5-shim.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@
// ========
//


// ES-5 15.3.4.5
// http://es5.github.com/#x15.3.4.5

Expand Down Expand Up @@ -193,6 +194,49 @@ if ((supportsAccessors = owns(prototypeOfObject, "__defineGetter__"))) {
lookupSetter = call.bind(prototypeOfObject.__lookupSetter__);
}


// http://es5.github.com/#x15.3.4.3
// Fix Function.prototype.apply to work with generic array-like object instead of an array
;(function() {
var needsPatch;
try {
var result = (function (a) {return a}).apply(null, {0: 10, length: 1});
needsPatch = result !== 10;
} catch (exception) {
needsPatch = true;
}

if (needsPatch) {
Function.prototype.apply = (function (apply) {
return function (thisp, args) {
try {
//Try original first
return args != undefined ?
apply.call(this, thisp, args) :
apply.call(this, thisp);
}
catch (e) {//"Function.prototype.apply: Arguments list has wrong type"
if(args.length === void 0 || typeof args === "string")throw e;

//toArray: (IE < 9 Not support `Array.prototype.slice.call` on DOM object)
args = toObject(args);
var i = -1,
arr = [],
length = args.length >>> 0;

while (++i < length) {
if (i in args) {
arr.push(args[i])
}
}

return apply.call(this, thisp, arr);
}
}
})(Function.prototype.apply);
}
})();

//
// Array
// =====
Expand Down
65 changes: 65 additions & 0 deletions tests/spec/s-function.js
Original file line number Diff line number Diff line change
Expand Up @@ -134,4 +134,69 @@ describe('Function', function() {
expect(result).not.toBe(oracle);
});
});

describe('apply', function() {
var actual, expected,
testSubject;

testSubject = {
push: function(o) {
this.a.push(o);
}
};

function func() {
Array.prototype.forEach.call(arguments, function(a) {
this.push(a);
}, this);
return this;
};

beforeEach(function() {
actual = [];
testSubject.a = [];
});

it('Function.prototype.apply allow work with generic array-like object instead of an array', function() {
var result;
testSubject.func = function() {
try {
(function(a, b) { result = a + "" + b })
.apply(null, {0: 1, 1: 2, length: 2})
}
catch(e) {}
}.bind();
testSubject.func();
expect(result).toBe("12");
});

it('Function.prototype.apply allow work with generic array-like DOM object instead of an array', function() {
var result;
testSubject.func = function() {
try {
var div = document.createElement("div");
div.innerHTML = "<p>1</p><p>2</p>";

(function(a, b) { result = a.innerHTML + "" + b.innerHTML })
.apply(null, div.childNodes)
}
catch(e) {}
}.bind();
testSubject.func();
expect(result).toBe("12");
});

it('Function.prototype.apply avoid using string as a second parameter', function() {
var result = false;
testSubject.func = function() {
try {
(function(a, b) { return a + b })
.apply(null, "123")
}
catch(e) {result = true}
}.bind();
testSubject.func();
expect(result).toBe(true);
});
});
});