开发者

how to make an object an instanceof of its own prototype's property

开发者 https://www.devze.com 2023-04-11 07:46 出处:网络
taking a standard browser as example, there is the Window class in开发者_C百科stantiated as window variable

taking a standard browser as example,

there is the Window class in开发者_C百科stantiated as window variable

window variable also contains the Window constructor (window.Window)

test this in your (standard) browser:

alert(window instanceof window.Window);

var asd = function(){};
asd.prototype.test = asd;
var x = new asd();
alert(x instanceof x.test);

now, window is also instanceof EventTarget that is stored in window.EventTarget

how to inherit EventTarget in the window object?


I'm answering to myself :|

var EvtTarg = function(){};
EvtTarg.prototype.justATest = function(){alert("asd");};

var Win = function(){};
Win.prototype = Object.create(EvtTarg.prototype);
Win.prototype.EvtTarg = EvtTarg;
Win.prototype.Win = Win;

var win = new Win();
alert(win instanceof win.Win);
alert(win instanceof win.EvtTarg);

there is a better way to do this?

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号