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

Checking instanceof Arrays cross frames (needed for the vjs.Player.prototype.src method) #1218

Closed
wants to merge 7 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
2 changes: 1 addition & 1 deletion src/js/component.js
Original file line number Diff line number Diff line change
Expand Up @@ -471,7 +471,7 @@ vjs.Component.prototype.initChildren = function(){

if (children) {
// Allow for an array of children details to passed in the options
if (children instanceof Array) {
if (vjs.obj.isArray(children)) {
for (var i = 0; i < children.length; i++) {
child = children[i];

Expand Down
11 changes: 11 additions & 0 deletions src/js/lib.js
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,17 @@ vjs.obj.isPlain = function(obj){
&& obj.constructor === Object;
};

/**
* Check if an object is Array
* Since instanceof Array will not work on arrays created in another frame we need to use Array.isArray, but since IE8 does not support Array.isArray we need this shim
* @param {Object} obj Object to check
* @return {Boolean} True if plain, false otherwise
* @private
*/
vjs.obj.isArray = Array.isArray || function(arr) {
return Object.prototype.toString.call(arr) === '[object Array]';
};
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You should add a new line between here and the next comment.


/**
* Bind (a.k.a proxy or Context). A simple method for changing the context of a function
It also stores a unique id on the function so it can be easily removed from events
Expand Down
2 changes: 1 addition & 1 deletion src/js/player.js
Original file line number Diff line number Diff line change
Expand Up @@ -1081,7 +1081,7 @@ vjs.Player.prototype.src = function(source){
}

// Case: Array of source objects to choose from and pick the best to play
if (source instanceof Array) {
if (vjs.obj.isArray(source)) {

var sourceTech = this.selectSource(source),
techName;
Expand Down
8 changes: 8 additions & 0 deletions test/unit/lib.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,14 @@ test('should copy an object', function(){
deepEqual(asdf,fdsa);
});

test('should check if an object is an Array', function(){
var arr = ['a', 'b', 'c'];
ok(vjs.obj.isArray(arr) === true, 'Arr object is an Array');

var obj = {};
ok(vjs.obj.isArray(obj) === false, 'Obj is not an Array');
});

test('should check if an object is plain', function(){
var empty = {};
ok(vjs.obj.isPlain(empty) === true, 'Empty object is plain');
Expand Down