简体   繁体   English

从Object调用Object.Prototype中的函数

[英]Calling functions in Object.Prototype from Object

我使用Object.prototype.func = function () {...}Object.prototype添加了一个函数,但是当我尝试使用Object.func()对其进行调用时,会引发错误,该错误未在Object中定义。

Everything in JavaScript is an object and inherits from Object (including Object). JavaScript中的所有内容都是一个对象,并且继承自Object(包括Object)。 If there's a property on Object.prototype then EVERYTHING can access it because it is in everything's prototype. 如果Object.prototype上有一个属性,那么任何东西都可以访问它,因为它存在于所有原型中。

http://jsfiddle.net/PxLDu/1/ http://jsfiddle.net/PxLDu/1/

Seems to be working fine for me (from node REPL): 似乎对我来说工作正常(来自节点REPL):

Object.prototype.something = function() { console.log("Something!"); }
[Function]
> b = new Ob
Object  

Object  

> b = new Object()
{}
> b.something()
Something!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM