TIL coding
  • Initial page
  • 排版
  • Flexbox
  • Grid
  • jQuery
  • Untitled
  • JavaScript
    • An Introduction to JavaScript
    • Hello, world!
    • Code structure
    • The modern mode, "use strict"
    • Variables
    • Data types
    • Type Conversions
    • Operators
    • Comparisons
    • Interaction: alert, prompt, confirm
    • Conditional operators: if, '?'
    • Logical operators
    • Loops: while and for
    • The "switch" statement
    • Functions
    • Function expressions and arrows
    • JavaScript specials
    • Comments
    • Ninja code
    • Automated testing with mocha
    • Polyfills
    • Objects
    • Garbage collection
    • Symbol type
    • Object methods, "this"
    • Object to primitive conversion
    • Constructor, operator "new"
    • Methods of primitives
    • Numbers
    • Strings
    • Arrays
    • Array methods
    • Iterables
    • Map, Set, WeakMap and WeakSet
    • Object.keys, values, entries
    • Destructuring assignment
    • Date and time
    • JSON methods, toJSON
    • Recursion and stack
    • Rest parameters and spread operator
    • Closure
    • The old "var"
    • Global object
    • Function object, NFE
    • The "new Function" syntax
    • Scheduling: setTimeout and setInterval
    • Decorators and forwarding, call/apply
    • Function binding
    • Currying and partials
    • Arrow functions revisited
    • Property flags and descriptors
    • Property getters and setters
    • Prototypal inheritance
    • F.prototype
    • Native prototypes
    • Prototype methods, objects without __proto__
    • The “class” syntax
    • Class inheritance
    • Static properties and methods
    • Private and protected properties and methods
    • Extending built-in classes
    • Class checking: "instanceof"
    • Mixins
    • Error handling, "try..catch"
    • Custom errors, extending Error
    • Introduction: callbacks
    • Promise
    • Promises chaining
    • Error handling with promises
    • Promise API
  • Bootstrap
    • Navbar
Powered by GitBook
On this page
  • A primitive as an object
  • Constructors String/Number/Boolean are for internal use only
  • null/undefined have no methods

Was this helpful?

  1. JavaScript

Methods of primitives

A primitive as an object

JavaScript 作者遇到的難題:

  • 人們想用方法對原始值做很多事情。

  • 原始值必須要輕量且快速。

解決方法如下:

  • 原始值仍為原始值,只能儲存單一值。

  • 語法允許原始值有方法、屬性,如同物件一樣。

  • 當使用這些方法時,JavaScript 提供特殊的包裝物件,執行完畢便會消失。

// string method
let str = "Hello";
alert( str.toUpperCase() ); // HELLO

// number method
let n = 1.23456;
alert( n.toFixed(2) ); // 1.23
  • 當使用 toUpperCase() ,JavaScript 創造一個特殊包裝物件,

  • 方法執行並返回運算結果。

  • 特殊包裝物件消失,原本的 str 仍然存在。

Constructors String/Number/Boolean are for internal use only

Javascript 允許創造特殊包裝物件的建構函式 String/Number/Boolean 但高度不建議這樣做。

// create new wrapper-object
alert( typeof 0 ); // "number"
alert( typeof new Number(0) ); // "object"!

let zero = new Number(0);
if (zero) { // zero is true, because it's an object
  alert( "zero is truthy!?!" );
}

// type convert
let num = Number("123"); // convert a string to number

null/undefined have no methods

null and undefined 沒有特殊包裝物件。

alert(null.test); // error
PreviousConstructor, operator "new"NextNumbers

Last updated 5 years ago

Was this helpful?