[英]Methods in JavaScript?
So I made a JavaScript Object, which makes an element in the DOM, and what I use my methods for, is to for example play the audio, set the duration of the audio etc. However, a method that I made inside my object apparently doesn't exist. 因此,我制作了一个JavaScript对象,该对象在DOM中构成了一个元素,我使用的方法是例如播放音频,设置音频的持续时间等。但是,显然,我在对象内部制作的一种方法不存在。 By the way, I'm new, so I don't really know what I did wrong... But here is my code:
顺便说一句,我是新手,所以我真的不知道自己做错了什么...但这是我的代码:
function Audio(paramSource) {
var object = document.createElement("audio");
(...)
function play() {
object.play();
};
(...)
};
var myAudio = new Audio("http://tufda.net/space/limewire.mp3");
myAudio.play();
Thanks in advance :)! 提前致谢 :)!
Close, you need to assign it as a property to the object. 关闭,您需要将其作为属性分配给对象。 Something like this:
像这样:
function Audio(paramSource) {
var object = document.createElement("audio");
//...
this.play = function() {
object.play();
};
//...
};
A function can be declared like a variable, and for the Audio
object to publicly expose it (instead of just being inside its own scope) you would simply set that variable to a property on this
within that scope. 可以像声明一个变量一样声明一个函数,并且
Audio
对象公开公开它(而不是仅仅位于其自己的作用域之内),您只需将该变量设置this
域内的属性即可。
So in the above code, object
is a variable internal to the scope of Audio
and play
is a property on Audio
. 因此,在上面的代码中,
object
是一个可变的内部的范围Audio
和play
是在属性上 Audio
。
You can assign to the object the object
var: 您可以将
object
var分配给该对象:
function Audio(paramSource) {
this.object = document.createElement("audio");
(...)
this.play = function() {
this.object.play();
};
(...)
};
var myAudio = new Audio("http://tufda.net/space/limewire.mp3");
myAudio.play();
By this way you can access to Audio.object
from outside. 这样,您可以从外部访问
Audio.object
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.