字符串对象? [英] Are strings objects?

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

问题描述

认为字符串是对象有两个原因。首先,您可以通过以下方式创建字符串:

Here are two reasons to think strings are objects. First, you can create a string in the following way:

var mystring = new String("asdf");

我的印象是新操作符后面的构造函数必须返回一个对象。其次,字符串似乎有属性和方法。例如:

I'm under the impression that the constructor function following the new operator has to return an object. Second, strings seem to have properties and methods. For example:

mystring.toUpperCase();

但是,如果字符串是对象,那么我们期望以下内容可以工作:

BUT, if strings were objects, then we'd expect something like the following to work:

function string_constructor() {
return "asdf";
}

var mystring = new string_constructor();

但它没有,我被告知它没有,因为字符串不是对象。字符串对象是否也是如此?而且,无论哪种方式,我如何理解我列出的所有内容?

But it doesn't, and I've been told it doesn't because strings aren't objects. So are strings objects or not? And, either way, how can I make sense of everything I've listed?

推荐答案

谈到语言类型,字符串是字符串类型的

Speaking about language types, Strings are values of the String type.

该语言有五种 原始类型 ,是 String Number Boolean Null Undefined

The language has five primitive types, which are String, Number, Boolean, Null and Undefined.

有String对象(也用于Number或Boolean),它们被称为原始包装器,它们是在使用与它们相关联的构造函数时创建的,用于例如:

There are String objects (also for Number or Boolean), they are called primitive wrappers, they are created when you use the constructor function associated with them, for example:

typeof new String('foo'); // "object"
typeof 'foo';             // "string"

但是不要与它们混淆,你很少 使用原始包装器,因为即使原始值不是对象,您仍然可以访问其继承的属性,例如,在字符串上,您可以访问 String.prototype的所有成员,例如:

But don't get confused with them, you will rarely need to use primitive wrappers, because even if primitive values are not objects, you can still access their inherited properties, for example, on a string, you can access all members of String.prototype, e.g.:

'foo'.indexOf('o'); // 2

这是因为属性访问器(本例中为点)暂时将原始值转换为一个对象,用于解析原型链中的 indexOf 属性。

That's because the property accessor (the dot in this case) temporarily converts the primitive value to an object, for being able to resolve the indexOf property up in the prototype chain.

关于构造函数你在您的问题中,如您所知,它不会返回字符串。

About the constructor function you have in your question, as you know, it won't return the string.

使用 new 调用的函数operator返回一个隐式值,这是一个从该函数的 prototype 继承的新对象,例如:

Functions called with the new operator return an implicit value, which is a new object that inherits from that function's prototype, for example:

function Test () {
  // don't return anything (equivalent to returning undefined)
}

new Test() instanceof Test; // true, an object

如果从构造函数返回对象 ,新创建的对象(在构造函数中)将丢失,因此显式返回的对象将出现函数:

If an object is returned from the constructor, that newly created object (this within the constructor) will be lost, so the explicit returned object will come out the function:

function Test2() {
  return {foo: 'bar'};
}

new Test2().foo; // 'bar'

但是对于原始值,它们是只是被忽略,隐式返回构造函数中的新对象(有关更多详细信息,请查看 [[Construct]] 内部操作,(见步骤9和10))。

But in the case of primitive values, they are just ignored, and the new object from the constructor is implicitly returned (for more details check the [[Construct]] internal operation, (see step 9 and 10)).

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

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