简体   繁体   English

Javascript中的String构造函数如何返回没有键的Object?

[英]How does the String constructor function in Javascript return an Object without a key?

When I do new String("hello") in my console I get the following result: 当我在控制台中执行new String("hello") ,得到以下结果:

String {"hello", length: 5}

The first value in the String Object that was returned doesn't have a key. 返回的String对象中的第一个值没有键。

Since objects are associative/hash arrays ie defined and created with keys and values how is the String constructor returning an object with a value without key? 由于对象是关联/哈希数组,即使用键和值定义和创建,因此String构造函数如何返回不带键的值的对象?

It doesn't. 没有。

You're just misinterpreting the console's visualisation of a string object and assuming it is expressing it in the form of object literal syntax. 您只是误解了控制台对字符串对象的可视化,并假定它以对象文字语法的形式表示它。 If it would, you would see: 如果可以,您将看到:

 { 0: "h", 1: "e", 2: "l", 3: "l", 4: "o", length: 5, __proto__: String }

But thats certainly quite unreadable, so they probably changed the way of visualizing to make it easier to read. 但这肯定是不可读的,因此他们可能改变了可视化方式,以使其更易于阅读。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何从JavaScript中的构造函数返回字符串? - how to return string from constructor function in javascript? 如何在 ZDE9B9ED78D7E2E911DCEEFFEE780Z 中的当前 function 上返回 function 构造函数 object 属性? - How to return function constructor object property on the current function in javascript? 如何从JavaScript中的自定义对象构造函数返回视频时长? - How to return video duration from custom object constructor function in javascript? 使用字符串“构造函数”作为JavaScript对象中的键 - Using string “constructor” as key in JavaScript object 在JavaScript中,“返回”在函数构造函数中起什么作用 - In JavaScript, what does “return” do in function constructor 构造函数 object 总是返回 function 而不是字符串 - Constructor object always return a function not a string 'this'如何在构造函数中工作以在Javascript中创建对象 - How does 'this' work in constructor function for Object creation in Javascript 函数不会在javascript中返回对象 - Function does not return object in javascript JavaScript 将字符串“constructor”转换为 [Function: Object] - JavaScript converting the string "constructor" to [Function: Object] 为什么构造函数(带有“ return this”)返回窗口对象? - Why does constructor function (with “return this”) return window object?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM