I'm working on a custom video codec for Javascript that supports transparency. I'm at the point of developing an API for the codec, and I would like to match the html video API as much as possible.
So with regular video you can play a video element by chaining the "play" function to the element itself:
document.getElementById("myVideo").play();
You can also access certain object settings within the element and change them such as the video source:
document.getElementById("myVideo").src = "movie.mp4";
I want to achieve this but with my own custom object between the HTML element and the function/object you are referencing to avoid name conflicts. In practice it would look like this:
document.getElementById("myVideo").myObject.play();
document.getElementById("myVideo").myObject.src = "movie.mp4";
However I can't think of a way to make this work using the same "myObject". myObject can be a constructor function which would return a new object, or it can be an object with functions/values stored inside.
Returning a function would work well for the "play" function because it could have access to the html element that called it.
HTMLElement.prototype.myObject= function(){
var objInstance = {};
objInstance.documentElement = this;
objInstance.play = function() { alert(this.documentElement) };
return objInstance;
}
"this" is referencing the html element which called the function. However doing this means that setting an attritubute like "src" won't work externally because the function is only a constructor that returns a new object. I would also like to have source be an object which can be externally exposed like this:
HTMLElement.prototype.myObject = {
documentElement: HTMLElement,
src: "hi",
play () {
console.log(this.documentElement);
}
}
Now I can edit "src" externally, and call the play function, however in this case the play function no longer has access to the html element. Instead the documentElement is storing the window object for the entire page.
How can I implement both these features above in the same variable?
myObjecthere is just a glorified global object anyway, there is no instance creation here, and even if you do make it so it does, having everyHTMLElementhaving an instance ofmyObjectwould be very wasteful. It would make much more sense to create a wrapper Object.