gpt4 book ai didi

javascript - String 是否继承自 Javascript 中的 Object?

转载 作者:行者123 更新时间:2023-12-01 14:44:37 36 4
gpt4 key购买 nike

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

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

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

那么,s1s2 是不同的类型吗?

最佳答案

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

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.

最后:

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.

阅读up more here .

关于javascript - String 是否继承自 Javascript 中的 Object?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2811092/

36 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com