最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

prototypal inheritance - How do you remove a method from a javascript type - Stack Overflow

programmeradmin2浏览0评论

I have a bit of javascript that runs on a 3rd party's website which requires me to temporarily add a function to the Array type, e.g.

Array.prototype.foo = function() { alert("foo's for everyone!"); }; 

I want to be a good developer and not pollute the type systems of other people's code as much as possible so once the function is no longer needed I want to remove the function again. Is this possible?

I have a bit of javascript that runs on a 3rd party's website which requires me to temporarily add a function to the Array type, e.g.

Array.prototype.foo = function() { alert("foo's for everyone!"); }; 

I want to be a good developer and not pollute the type systems of other people's code as much as possible so once the function is no longer needed I want to remove the function again. Is this possible?

Share Improve this question edited Jun 20, 2012 at 22:48 James Allardice 166k22 gold badges334 silver badges315 bronze badges asked Jul 25, 2011 at 9:03 James HollingworthJames Hollingworth 14.2k12 gold badges40 silver badges58 bronze badges
Add a comment  | 

2 Answers 2

Reset to default 21

You can use delete, which in general is good for removing all types of properties from objects:

delete Array.prototype.foo;

Example: http://jsbin.com/iyamut
MDN Documentation: delete

Array.prototype.foo = undefined

or

delete Array.prototype.foo
发布评论

评论列表(0)

  1. 暂无评论