【发布时间】:2021-12-29 19:08:53
【问题描述】:
在这里学习 JS 的初学者。我不明白为什么当你声明一个变量时它不会完全继承它的父类方法,例如:
// I initiate an array (my question is the same for all type of vars)
var myArr = ["foo", "bar"]
// Let's say I call a random function of the parent class Array
console.log(Array.isArray(myArr)); // true
// Since I assume that myArr inherited of the COMPLETE LIST of Array's methods, I should be able to do this:
console.log(myArr.isArray()); // Uncaught TypeError
为什么变量不继承其父类的所有方法?取而代之的是,您需要在 Array 和 myArr 的函数之间进行混合。两边应该是一样的吧?
【问题讨论】:
-
Array.isArray() 未在 Array 的原型上定义,因此在创建新数组时不会被继承。有一些区别,但从概念上讲,它很像static class method。
标签: javascript function class variables inheritance