修改String.prototype? [英] Modify String.prototype?

查看:60
本文介绍了修改String.prototype?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

常见问题解答显示了String.prototype的一个mod添加了trim,trimRight和

trimLeft。


弗拉纳根州,有一个强烈反对的论点使用您自己的方法在类型中扩展内置

;如果你这样做,你基本上是创建自己的JavaScript API版本。然后他接着说b / b $ b这会让其他可能需要维护的程序员感到困惑/

扩展你的代码。


Flanagan还指出,你必须< i>永远< / iadd属性

Object.prototype。添加到Object.prototype的任何内容都会为每个对象添加可枚举的

属性,包括{}。他说,{}不应该有可枚举的b $ b属性。然而,Crockford经常违反这条规则。


最佳做法是?

解决方案

Ma ************ @ gmail.com 写道:


Flanagan还声明,你必须< i>从不< / iadd属性

Object.prototype。添加到Object.prototype的任何内容都会为每个对象添加可枚举的

属性,包括{}。他说,{}不应该有可枚举的b $ b属性。然而,克罗克福德经常违反这条规则。



例子?它不只是表明不该做什么?


最佳做法是什么?



不要。


/ L

-

Lasse Reichstein Nielsen

DHTML死亡颜色:< URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>

''没有信仰判断只会降低精神的神圣性。''


Lasse Reichstein Nielsen写道:


例子?它不只是表明不该做什么?



"在第3章中,我们看到可以扩充Object.prototype。

也可以扩充Array.prototype。页。 62

第3章示例:使用一个对象创建的beget()方法

另一个对象(同一个class)。


Ma *********** *@gmail.com 写道:


常见问题解答显示了String.prototype的mod添加trim,trimRight和

trimLeft 。



它应该将这些作为函数提供,因为

Flanagan提供的原因。


Flanagan表示,有一个强烈的论据反对用你自己的方法扩展内置的

;如果你这样做,你基本上是创建自己的JavaScript API版本。他继续说,

说这会让其他程序员感到困惑,他们可能需要维护/

扩展你的代码。



我们需要一个FAQ维护者。


Flanagan还声明,你必须< i>从不< / iadd属性

Object.prototype。添加到Object.prototype的任何内容都会为每个对象添加可枚举的

属性,包括{}。 {}不应该有可枚举的



那里没有分歧。


属性,他说。然而,Crockford经常打破这个规则。



是的,他确实。


最佳做法是什么?



修改内置插件的一个地方是添加对

功能测试为错误或丢失的功能的支持。


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?

解决方案

Ma************@gmail.com writes:

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.

Examples? It''s not just to show what not to do?

Best practice is?

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.''


Lasse Reichstein Nielsen wrote:

Examples? It''s not just to show what not to do?

"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").


Ma************@gmail.com wrote:

The FAQ shows a mod to String.prototype adding trim, trimRight and
trimLeft.

It should instead provide these as functions for the reasons provided by
Flanagan.

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.

We need an FAQ maintainer.

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

No disagreement there.

properties, he says. However, Crockford breaks this rule regularly.

Yes, he does.

Best practice is?

One place to modify built-ins is for adding support for features that
are feature-tested as buggy or missing.


这篇关于修改String.prototype?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆