问题描述
常见问题解答显示了String.prototype的一个mod添加了trim,trimRight和
trimLeft。
弗拉纳根州,有一个强烈反对的论点使用您自己的方法在类型中扩展内置
;如果你这样做,你基本上是创建自己的JavaScript API版本。然后他接着说b / b $ b这会让其他可能需要维护的程序员感到困惑/
扩展你的代码。
Flanagan还指出,你必须< i>永远< / iadd属性
Object.prototype。添加到Object.prototype的任何内容都会为每个对象添加可枚举的
属性,包括{}。他说,{}不应该有可枚举的b $ b属性。然而,Crockford经常违反这条规则。
最佳做法是?
例子?它不只是表明不该做什么?
不要。
/ L
-
Lasse Reichstein Nielsen
DHTML死亡颜色:< URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
''没有信仰判断只会降低精神的神圣性。''
"在第3章中,我们看到可以扩充Object.prototype。
也可以扩充Array.prototype。页。 62
第3章示例:使用一个对象创建的beget()方法
另一个对象(同一个class)。
它应该将这些作为函数提供,因为
Flanagan提供的原因。
我们需要一个FAQ维护者。
那里没有分歧。
是的,他确实。
修改内置插件的一个地方是添加对
功能测试为错误或丢失的功能的支持。
The FAQ shows a mod to String.prototype adding trim, trimRight and
trimLeft.
Flanagan states, "There is a strong argument against extending built-
in types with your own methods; if you do so, you are essentially
creating your own version of the JavaScript API." And he goes on to
say that this will confuse other programmers who may need to maintain/
extend your code.
Flanagan also states, "You must <i>never</iadd properties to
Object.prototype." Anything added to Object.prototype adds enumerable
properties to every object, including {}. {} shouldn''t have enumerable
properties, he says. However, Crockford breaks this rule regularly.
Best practice is?
Examples? It''s not just to show what not to do?
Don''t.
/L
--
Lasse Reichstein Nielsen
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
''Faith without judgement merely degrades the spirit divine.''
"In Chapter 3 we saw that Object.prototype can be augmented.
Array.prototype can be augmented as well." p. 62
Chapter 3 example: beget() method that uses one object to create
another object (of the same "class").
It should instead provide these as functions for the reasons provided by
Flanagan.
We need an FAQ maintainer.
No disagreement there.
Yes, he does.
One place to modify built-ins is for adding support for features that
are feature-tested as buggy or missing.
这篇关于修改String.prototype?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!