内置类型的'new'运算符? [英] 'new' operator for built-in types?
问题描述
嗨。
是否有必要对内置类型使用''new''运算符,如String,
Number,RegExp ,. ....?结果似乎与没有''new''的
相同。
例如在一行中看起来像这样:
s = s.replace(RegExp(" \\\\ * \\ b" + name +" \\\\\ *"),"") ;
谢谢,
Dano
Daniel Norden< dn **** **** @ gmail.com编写:
是否有必要对String这样的内置类型使用''new''运算符,
Number,RegExp,.....?结果似乎与没有''new''的$ b / b相同。
这取决于。
对于RegExp和函数,将其作为函数和构造函数调用
做同样的事情,即创建一个新对象。
对于Date,Object,String,Boolean和Number,作为函数调用
不会创建一个新对象。相反,最后四个执行转换,
并且我不记得Date作为函数调用时的作用。我有
可能从未使用过它。
例如在一行中看起来像这样:
s = s.replace(RegExp(" \\s * \\b" + name +" \\\\\ *"),"");
/ L
-
Lasse Reichstein Holst Nielsen
DHTML死亡颜色:< URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
''没有判断的信仰只会降低精神神圣。''
11月4日上午8:04 *,Daniel Norden< dnorden ... @ gmail.comwrote:
嗨。
是否有必要对内置类型使用''new''运算符,如String,
Number,RegExp,.....?结果似乎与没有''new''的$ b / b相同。
这取决于你所说的必要。调用String()作为
函数进行类型转换,将其作为新表达式的一部分调用
创建一个新对象(ECMA-262 Section 15.5.1。& 15.5.2):
var x = String();
var y = new String();
alert( ''x是''+ typeof x + // String
''\ n''+''y是''+ typeof y); //对象
例如,在如下所示的行中:
s = s.replace(RegExp(" \\\\ * \\b" + name +" \\\\\\ * *"),"");
RegExp函数不同,当作为函数调用时,它可能会像在新表达式中被调用一样表现,细节是在
ECMA-262第15.10.3节。
| 15.10.3.1 RegExp(模式,标志)
|如果pattern是一个对象R,其[[Class]]属性
|是RegExp和标志是未定义的,然后返回R
|不变。否则调用RegExp构造函数
| (第15.10.4.1节),将模式和标志传递给
|参数并返回由该构造函数构造的对象。
-
Rob
Lasse Reichstein Nielsen写道:
对于RegExp和Function,将其作为函数和构造函数调用
执行相同的操作,即创建新对象。 br />
对于Date,Object,String,Boolean和Number,作为函数调用
不会创建新对象。相反,最后四个执行转换,
并且我不记得Date作为函数调用时的作用。我有点b $ b可能从未使用它。
我刚检查过,Date()返回一个时间戳字符串。
感谢您的解释,Lasse。
>
Dano
Hi.
Is it necessary to use the ''new'' operator for built-in types like String,
Number, RegExp, .....? The result seems to be the same with or
without ''new''.
For example in a line that looks like this:
s = s.replace(RegExp("\\s*\\b" + name + "\\b\\s*"), " ");
Thanks,
Dano
Daniel Norden <dn********@gmail.comwrites:
Is it necessary to use the ''new'' operator for built-in types like String,
Number, RegExp, .....? The result seems to be the same with or
without ''new''.That depends.
For RegExp and Function, calling it as a function and as a constructor
does the same thing, i.e., creates a new object.
For Date, Object, String, Boolean and Number, calling as a function
doesn''t create a new object. Instead the last four perform conversion,
and I don''t remember what Date does when called as a function. I have
probably never used it.
For example in a line that looks like this:
s = s.replace(RegExp("\\s*\\b" + name + "\\b\\s*"), " ");/L
--
Lasse Reichstein Holst Nielsen
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
''Faith without judgement merely degrades the spirit divine.''
On Nov 4, 8:04*am, Daniel Norden <dnorden...@gmail.comwrote:Hi.
Is it necessary to use the ''new'' operator for built-in types like String,
Number, RegExp, .....? The result seems to be the same with or
without ''new''.It depends on what you mean by "necessary". Calling String() as a
function does type conversion, calling it as part of a new expression
creates a new object (ECMA-262 Section 15.5.1. & 15.5.2):
var x = String();
var y = new String();
alert( ''x is a '' + typeof x + // String
''\n'' + ''y is a '' + typeof y); // Object
For example in a line that looks like this:
s = s.replace(RegExp("\\s*\\b" + name + "\\b\\s*"), " ");The RegExp function is different, when called as a function it may
behave as if called as part of a new expression, details are in
ECMA-262 Section 15.10.3.
| 15.10.3.1 RegExp(pattern, flags)
| If pattern is an object R whose [[Class]] property
| is "RegExp" and flags is undefined, then return R
| unchanged. Otherwise call the RegExp constructor
| (section 15.10.4.1), passing it the pattern and flags
| arguments and return the object constructed by that constructor.
--
Rob
Lasse Reichstein Nielsen wrote:For RegExp and Function, calling it as a function and as a constructor
does the same thing, i.e., creates a new object.
For Date, Object, String, Boolean and Number, calling as a function
doesn''t create a new object. Instead the last four perform conversion,
and I don''t remember what Date does when called as a function. I have
probably never used it.I just checked, Date() returns a timestamp string.
Thanks for the explanation, Lasse.
Dano
这篇关于内置类型的'new'运算符?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!