简体   繁体   中英

Conflict of two javascript files

I'm playing with html5 LeanBack Player and it works well when on the page only its javascritpts. But if I add these javascript files to rails 3 project then I get error in LeanBack Player's javascript file:

Uncaught TypeError: Object function each(iterator, context) {
  var index = 0;
  try {
    this._each(function(value) {
      iterator.call(context, value, index++);
    });
  } catch (e) {
    if (e != $break) throw e;
  }
  return this;
} has no method ‘split’

in function LBPlayer.prototype.resolveTextPlainSubs.

I guessed that's because of conflict with native Prototype in rails but I don't know how to resolve it. Thanks

Indeed it's a conflict with Prototypes extending of Array.prototype :

// leanbackPlayer.js @941
srt = srt.split('\n\n');
var i = 0; var isSub = false;
this.vars.subs[lang] = {};
this.vars.subs[lang].label = {};
this.vars.subs[lang].label = label;
this.vars.subs[lang].track = {};

// Error: using for in over an array is just stupid
//        not even using hasOwnProperty is outright pitiful
for(var s in srt) { 

    // this will also yield `each` but that's a function which has no .split() method
    var st = srt[s].split('\n');
    var time; var j;
    if(st.length >= 2) {
        var t = "";

In order to fix it you should replace the for(var s in srt) with a simple for loop:

for(var e = 0, el = srt.length; e < el; e++) {
    var st = srt[e].split('\n');
    var time; var j;
    if(st.length >= 2) {
        var t = "";
        ...
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM