object.prototype打破javascript

时间:2017-02-17 16:34:40

标签: javascript jquery

我会尽力解释这个问题。如果这没有意义,我道歉。

"的 Object.prototype中"似乎打破了我的JavaScript,我不知道如何解决这个问题。我使用的是YouTube api,我创建了一个循环播放10个视频(xml文件)的脚本并重复播放。

如果你看一下javascript底部的代码,你会注意到我有&#34; Object.prototype.getKey = function(value){... < / strong>&#34;

该代码在我的网页上打破了javascript的其他部分。如果我删除了...然后打破我的YouTube播放器,不要循环到下一个视频。所以我无法做到这一点。

这是错误:

Uncaught TypeError: X[g].exec is not a function
    at fb.tokenize (jquery-latest.min.js:2)
    at Function.fb [as find] (jquery-latest.min.js:2)
    at m.fn.init.find (jquery-latest.min.js:2)
    at HTMLDivElement.<anonymous> (photoAds-slider.js:37)
    at Function.each (jquery-latest.min.js:2)
    at m.fn.init.each (jquery-latest.min.js:2)
    at m.fn.init.d.fn.flexslider (photoAds-slider.js:37)
    at HTMLDocument.<anonymous> (screen.php:44)
    at j (jquery-latest.min.js:2)
    at Object.fireWith [as resolveWith] (jquery-latest.min.js:2)

无论如何?

PHP

// ========================================
// Load the xml files for youtube player (YouTube IDs)
// ========================================

$xml1=simplexml_load_file("youtube/1.xml");
$youtube1 = $xml1->video;

$xml2=simplexml_load_file("youtube/2.xml");
$youtube2 = $xml2->video;

$xml3=simplexml_load_file("youtube/3.xml");
$youtube3 = $xml3->video;

$xml4=simplexml_load_file("youtube/4.xml");
$youtube4 = $xml4->video;

$xml5=simplexml_load_file("youtube/5.xml");
$youtube5 = $xml5->video;

$xml6=simplexml_load_file("youtube/6.xml");
$youtube6 = $xml6->video;

$xml7=simplexml_load_file("youtube/7.xml");
$youtube7 = $xml7->video;

$xml8=simplexml_load_file("youtube/8.xml");
$youtube8 = $xml8->video;

$xml9=simplexml_load_file("youtube/9.xml");
$youtube9 = $xml9->video;

$xml10=simplexml_load_file("youtube/10.xml");
$youtube10 = $xml10->video;


$volume = '100';
$quality = '720';

$videos = array("$youtube1", "$youtube2", "$youtube3", "$youtube4", "$youtube5", "$youtube6", "$youtube7", "$youtube8", "$youtube9", "$youtube10",);

// print_r(array_values($videos));


JAVSCRIPT

playlistids = <?php echo json_encode($videos); ?>;

  var playing = playlistids[0];
  var ytplayer;

  function onYouTubeIframeAPIReady() {
    ytplayer = new YT.Player('ytapiplayer', {
        width: '100%',
        height: '100%',
        videoId: playing,
        events: {
            'onStateChange': onytplayerStateChange,
            'onReady': onPlayerReady
        },
          playerVars: { 
           'controls': 0,
           // 'cc_load_policy': 1,
           'rel' : 0,
           'showinfo' : 0,
          }
    });
}


function onPlayerReady(event) {
    event.target.setPlaybackQuality(<?php echo $quality; ?>)
    event.target.setVolume(<?php echo $volume; ?>)
    // alert('Start playing entry #0 (preloaded).')
    event.target.playVideo()

    // Fade out music player
    audio_fade_out();

}

function play(ytid) {
    if (ytplayer) {
        playing = ytid
        // alert('On to entry #'+ nextentrykey +', playing set to: ' + playing)
        ytplayer.loadVideoById(ytid, 0, <?php echo $quality; ?>);
    }
}


function onytplayerStateChange(event) {
  //alert('NEW STATE: ' + event.data)
    if ( event.data == 0 ) {
       setTimeout(youtubeTimer, 4200);

        // Fade in music player
        audio_fade_in();

        // alert('Since the new player state is '+ event.data +', the video has ended. Getting next key after playing ' + playing + '.');
        nextentrykey = parseInt(playlistids.getKey(playing))+1
        if (nextentrykey >= playlistids.length) {
                nextentrykey = 0
        }
        document.getElementById("ytapiplayer").style.visibility = "hidden";
    }
}


function youtubeTimer() {
        play(playlistids[nextentrykey]);
        document.getElementById("ytapiplayer").style.visibility = "visible";

        // Fade out music player
        audio_fade_out();
}


Object.prototype.getKey = function(value){
  for(var key in this){
    if(this[key] == value){
      return key;
    }
  }
  return -1;
};




} else {
  // alert('YouTube is OFF')
  document.getElementById("ytapiplayer").style.visibility = "hidden";
}

1 个答案:

答案 0 :(得分:1)

扩展内置原型是不好的做法。它可以破坏期望不会发生的脚本,例如jQuery。

你没有理由这样做。改为创建一个普通函数:

function getKey(obj, value) {
  // ...
}

并使用getKey(playlistids, playing)代替playlistids.getKey(playing)