删除不必要的重复
Removing unnecessary duplication
我正在尝试从默认设置中删除这些 playerVars,因为这是不必要的重复。
start: 0,
end: 999999,
loop: true,
https://jsfiddle.net/hzyrfkwb/461/
function addVideo(video, settings) {
const defaultSettings = {
width: settings.width || 640,
height: settings.height || 390,
videoId: video.dataset.id,
playerVars: {
start: 0,
end: 999999,
loop: true,
有没有办法让我只在需要时添加它们?
如本例所示:
https://jsfiddle.net/hzyrfkwb/432/
这样做会消除很多不必要的重复。
loadPlayer({
target: ".jacketc",
width: 600,
height: 338,
playerVars: {
start: 200,
end: 205,
loop: true
}
});
不需要在上面都注明:
function addVideo(video, settings) {
const defaultSettings = {
width: settings.width || 640,
height: settings.height || 390,
videoId: video.dataset.id,
playerVars: {
start: 0,
end: 999999,
loop: true
还有底部:
loadPlayer({
target: ".jacketc",
width: 600,
height: 338,
playerVars: {
start: 200,
end: 205,
loop: true
}
});
这些只应在需要时添加到 loadPlayer。
在默认设置中,不应要求它们位于顶部。
start: 0,
end: 999999,
loop: true,
这些应该只是默认设置:
autoplay: 1,
controls: 1,
showinfo: 1,
rel: 0,
iv_load_policy: 3,
cc_load_policy: 0,
fs: 0,
disablekb: 1
};
这些只会在需要时添加到 loadPlayer。
如果不需要,则无需出现在 javascript.
中
start: 0,
end: 999999,
loop: true,
我怎样才能对代码实施这种调整?
https://jsfiddle.net/hzyrfkwb/453/
在默认设置中不需要这些 playerVars。
只有在需要时才会将它们添加到 loadPlayer。
start: 200,
end: 205,
loop: true,
这正是它在其他代码中的工作方式。
https://jsfiddle.net/hzyrfkwb/465/
我怎样才能在更新后的新代码中实现它?
我不太明白你的问题,但据我了解,你想在函数 addVideo()
中可选地设置 playerVars
但不想将 playerVars
定义为输入变量?也许你可以试试这个
["start", "end", "loop"].forEach(function (a) {
if (settings[a]) {
defaultSettings.playerVars[a] = settings[a];
}
});
// or
for (let a of ["start", "end", "loop"]) {
if (settings[a]) defaultSettings.playerVars[a] = settings[a];
}
// or
let optional = ["start", "end", "loop"];
for (let i = 0; i < optional.length; i++) {
if (settings[optional[i]]) {
defaultSettings.playerVars[optional[i]] = settings[optional[i]];
}
}
完整代码
function addVideo(video, settings) {
const defaultSettings = {
width: settings.width || 640,
height: settings.height || 390,
videoId: video.dataset.id,
playerVars: {
autoplay: 1,
controls: 1,
showinfo: 1,
rel: 0,
iv_load_policy: 3,
cc_load_policy: 0,
fs: 0,
disablekb: 1
},
events: {
"onReady": onPlayerReady,
"onStateChange": onPlayerStateChange
}
};
["start", "end", "loop"].forEach(function(a) {
if (settings[a]) defaultSettings.playerVars[a] = settings[a];
});
const updatedSettings = combineSettings(defaultSettings, settings);
console.log(updatedSettings)
players.push(new YT.Player(video, updatedSettings));
}
我正在尝试从默认设置中删除这些 playerVars,因为这是不必要的重复。
start: 0,
end: 999999,
loop: true,
https://jsfiddle.net/hzyrfkwb/461/
function addVideo(video, settings) {
const defaultSettings = {
width: settings.width || 640,
height: settings.height || 390,
videoId: video.dataset.id,
playerVars: {
start: 0,
end: 999999,
loop: true,
有没有办法让我只在需要时添加它们?
如本例所示:
https://jsfiddle.net/hzyrfkwb/432/
这样做会消除很多不必要的重复。
loadPlayer({
target: ".jacketc",
width: 600,
height: 338,
playerVars: {
start: 200,
end: 205,
loop: true
}
});
不需要在上面都注明:
function addVideo(video, settings) {
const defaultSettings = {
width: settings.width || 640,
height: settings.height || 390,
videoId: video.dataset.id,
playerVars: {
start: 0,
end: 999999,
loop: true
还有底部:
loadPlayer({
target: ".jacketc",
width: 600,
height: 338,
playerVars: {
start: 200,
end: 205,
loop: true
}
});
这些只应在需要时添加到 loadPlayer。 在默认设置中,不应要求它们位于顶部。
start: 0,
end: 999999,
loop: true,
这些应该只是默认设置:
autoplay: 1,
controls: 1,
showinfo: 1,
rel: 0,
iv_load_policy: 3,
cc_load_policy: 0,
fs: 0,
disablekb: 1
};
这些只会在需要时添加到 loadPlayer。 如果不需要,则无需出现在 javascript.
中 start: 0,
end: 999999,
loop: true,
我怎样才能对代码实施这种调整?
https://jsfiddle.net/hzyrfkwb/453/
在默认设置中不需要这些 playerVars。 只有在需要时才会将它们添加到 loadPlayer。
start: 200,
end: 205,
loop: true,
这正是它在其他代码中的工作方式。
https://jsfiddle.net/hzyrfkwb/465/
我怎样才能在更新后的新代码中实现它?
我不太明白你的问题,但据我了解,你想在函数 addVideo()
中可选地设置 playerVars
但不想将 playerVars
定义为输入变量?也许你可以试试这个
["start", "end", "loop"].forEach(function (a) {
if (settings[a]) {
defaultSettings.playerVars[a] = settings[a];
}
});
// or
for (let a of ["start", "end", "loop"]) {
if (settings[a]) defaultSettings.playerVars[a] = settings[a];
}
// or
let optional = ["start", "end", "loop"];
for (let i = 0; i < optional.length; i++) {
if (settings[optional[i]]) {
defaultSettings.playerVars[optional[i]] = settings[optional[i]];
}
}
完整代码
function addVideo(video, settings) {
const defaultSettings = {
width: settings.width || 640,
height: settings.height || 390,
videoId: video.dataset.id,
playerVars: {
autoplay: 1,
controls: 1,
showinfo: 1,
rel: 0,
iv_load_policy: 3,
cc_load_policy: 0,
fs: 0,
disablekb: 1
},
events: {
"onReady": onPlayerReady,
"onStateChange": onPlayerStateChange
}
};
["start", "end", "loop"].forEach(function(a) {
if (settings[a]) defaultSettings.playerVars[a] = settings[a];
});
const updatedSettings = combineSettings(defaultSettings, settings);
console.log(updatedSettings)
players.push(new YT.Player(video, updatedSettings));
}