Convention for prototype inheritance in JavaScript

前端 未结 3 1874
挽巷
挽巷 2020-12-10 16:24

I see a lot of code like this:

function Base() {}
function Sub() {}
Sub.prototype = new Base();

However, if you do:

s = new         


        
3条回答
  •  一个人的身影
    2020-12-10 16:59

    If you want to test whether an object is exactly an instance of Sub use the instanceof operator:-

    print(s instanceof Sub);
    

    If you want to know whether an object is an instance of Sub or an instance of a sub-class of Sub use the isPrototypeOf method:-

    print(Sub.prototype.isPrototypeOf(s));
    

提交回复
热议问题