字符串是否从Java中的Object继承? [英] Does String inherit from Object in Javascript?

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

问题描述

Object 对象,是Java语言中所有对象的基类,就像其他语言一样,例如Java& C#?

Is Object the base class of all objects in Javascript, just like other language such as Java & C#?

我在安装了Firebug的Firefox中尝试了以下代码.

I tried below code in Firefox with Firebug installed.

var t = new Object();
var s1 = new String('str');
var s2 = 'str';
console.log(typeof t);
console.log(typeof s1);
console.log(typeof s2);

控制台输出为

object
object
string

那么, s1 s2 是不同类型的吗?

So, s1 and s2 are of diffeent type?

推荐答案

是的,'str'是字符串literal,而不是字符串object.

Yes, 'str' is a string literal, not a string object.

字符串文字可以访问字符串的所有对象和方法,因为javascript会将字符串文字临时转换为字符串对象,以便运行所需的方法.

A string literal has access to all of a string's objects and methods because javascript will temporarily cast a string literal as a string object in order to run the desired method.

最后:

两者的区别在于对新特性和新方法的处理.像所有Javascript对象一样,您可以将属性和方法分配给任何String对象.您不能将属性或方法添加到字符串文字.解释器将忽略它们.

Where the two differ is their treatment of new properties and methods. Like all Javascript Objects you can assign properties and methods to any String object. You can not add properties or methods to a string literal. They are ignored by the interpreter.

在此处详细了解.

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

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