`

JavaScript ---- Wrapper Objects

 
阅读更多
We've also seen that strings have properties and methods:
     var s = "hello world!";        // A string
     var word = s.substring(s.indexOf(" ")+1, s.lenght);    // Use string properties
Strings are not objects, though, so why do they have properties? Whenever you try to refer to a property of a string s, JavaScript converts the string value to an object as if by calling new String(s).This object inherits string methods and is used to resolve the property reference. Once the property has been resolved, the newly created object is discarded.

Numbers and booleans have methods for the same reason that strings do: a temporary object is created using the Number() or Boolean() constructor, and the method is resolved using the temporary object.

There are not wrapped objects for the null and undefined values: any attempt to access a property of one of these values causes a TypeError.

The temporary objects created when you access a property of a string, number, or boolean are known as wrapped objects, and it may occasionally be necessary to distinguish a string value from a String object or a number or boolean value from a Number or Boolean object.

Note that it is possible (but almost never necessary or useful) to explicitly create wrapper objects, by invoking the String(), Number(), or Boolean() constructors:
     var s = "test", n = 1, b = true;        // A string, number, and boolean value.
      var S = new String(s);                  // A String object
      var N = new Number(n);                  // A Number object
      var B = new Boolean(b);                 // A Boolean object
分享到:
评论

相关推荐

    Javascript - The Definitive Guide 6th

    此外,书中还讲解了全局对象的作用和特点,以及封装基本数据类型的对象——包装对象(Wrapper Objects)的概念。 《Javascript - The Definitive Guide》第六版是一本完整的参考资料,不仅适用于初学者,也适合那些...

    [JavaScript权威指南(第6版)

    - **Wrapper Objects(包装对象)**:JavaScript中的`Number`、`String`、`Boolean`等构造函数实际上也是包装对象,它们将原始值封装成对象,以便于调用方法。 #### 四、书籍版本历史 自1996年首次出版以来,...

    javascript权威指南(第六版)

    3.6 Wrapper Objects 43 3.7 Immutable Primitive Values and Mutable Object References 44 3.8 Type Conversions 45 3.9 Variable Declaration 52 3.10 Variable Scope 53 4. Expressions and Operators . . . . ....

    Javascript_The_Definitive_Guide_6th

    最后,书中还提到了包装对象(Wrapper Objects),它们是JavaScript为了方便操作原始类型值(如数字和字符串)而创建的特殊对象。包装对象允许开发者在原始值上调用方法,就好像它们是对象一样。这为JavaScript的...

    EO.WebBrowser_for_.NET_2018.2.53.0

    You can execute JavaScript code and access all the JavaScript objects directly from your .NET code. Access their properties or even call a JavaScript function are all different options available to ...

    Manning jQuery in Action.pdf

    - **The jQuery Wrapper**: Every jQuery object is a wrapper around one or more DOM elements, which allows for chaining of methods and manipulation of these elements. - **Utility Functions**: jQuery ...

    jcef bulid x64

    It provides close integration between the browser and the host application including support for custom plugins, protocols, JavaScript objects and JavaScript extensions. The host application can ...

    python3.6.5参考手册 chm

    The json module: JavaScript Object Notation The plistlib module: A Property-List Parser ctypes Enhancements Improved SSL Support Deprecations and Removals Build and C API Changes Port-Specific ...

    php.ini-development

    ;;;;;;;;... 1.... 2.... 3.... 4.... 5.... 6.... The syntax of the file is extremely simple.... Section headers (e.g.... at runtime.... There is no name validation.... (e.g.... previously set variable or directive (e.g....

    Android代码-json

    Convenience library for handling JSON objects in Java. Goals: Usability Minimise verbosity Play well with Nashorn JavaScript engine from Java 8 In essence Json is just a thin wrapper around a Map ...

    Pro+Java+6+3D+Game+Development.part01

    Pro Java 6 3D Game Development: Java 3D, JOGL, JInput and JOAL APIs Create strange lands filled with mysterious objects (cows frozen in <br>blocks of ice, chirping penguins, golden globes with ...

    bre:JavaScript的二进制对象映射器

    JavaScript / TypeScript的对象二进制映射器。 这类似于带有存储块的C struct 。 import { defineObjectRecord } from "bre" // Define a Record Type. const TcpHeader = defineObjectRecord ( "TcpHeader" , {...

    外文翻译 stus MVC

    The page designer (or HTML developer) must understand colors, the customer, product flow, page layout, browser compatibility, image creation, JavaScript, and more. Putting a great looking site ...

    BobBuilder_app

    Twitter Digg Facebook Del.icio.us Reddit Stumbleupon Newsvine Technorati Mr....Database » Database » Other databasesLicence CPOL ...Even faster Key/Value store nosql embedded database ...

    Java学习笔记-个人整理的

    {12.18}\ttfamily user\_tables, user\_objects}{179}{section.12.18} {12.19}truncate}{179}{section.12.19} {12.20}alter}{180}{section.12.20} {12.21}constraint}{180}{section.12.21} {12.21.1}primary ...

    nested-datatables:jQuery DataTables插件,用于在行中呈现嵌套的DataTables。 内部表独立于外部表的数据和布局

    嵌套数据表 jQuery DataTables插件,用于在行中呈现嵌套的DataTables。 内部表独立于外部表的数据和布局。 安装 ...TableHierarchy(wrapperID,数据,设置) 主要的NestedTables构造函数。 wrapperI

    Github:Scorm文件

    2. **数据模型(Data Model)**:SCORM定义了数据模型,如SCO(Shareable Content Objects,可共享内容对象)和SCO集合,以及如何存储和访问这些数据。 3. **API(Application Programming Interface)**:LMS提供...

    acquit:解析BDD样式的测试(Mocha,Jasmine)以生成文档

    开释 解析BDD样式的测试... * A Model is a convenience wrapper around objects stored in a * collection */ describe('Model', function() { /** * Model **should** be able to save **/ it('can save'

Global site tag (gtag.js) - Google Analytics