是否可以使Object.prototype为空?

Shu*_*ham 1 javascript

我正在检查一些JS库代码并且知道他们正在使用原型函数从主对象Object.prototype.toString.call(a, this)(这里是一个对象).那么有可能null在主要的Object原型中分配,比如Object.prototype = null

Vas*_*san 5

Object.prototype 不可写.所以不行.事实上,没有任何内置原型是可写的.

你可以在这里试试:

console.log(typeof Object.prototype.toString); //"function"
Object.prototype = null;
console.log(typeof Object.prototype.toString); //not nullified - still "function"

console.log(typeof String.prototype.substring); //"function"
String.prototype = null;
console.log(typeof String.prototype.substring); //same
Run Code Online (Sandbox Code Playgroud)