Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
tree: 032000f07e
Fetching contributors…

Cannot retrieve contributors at this time

54 lines (46 sloc) 1.293 kb
<!doctype html>
<html lang="en">
<head>
<title>JavaScript Patterns</title>
<meta charset="utf-8">
</head>
<body>
<script>
/* Title: Primitive Wrappers
* Description: JavaScript has 3 primitive wrapper objects: number, string, boolean
*/
// antipattern
// with wrappers
var s = new String("my string");
var n = new Number(101);
var b = new Boolean(true);
// preferred
// without wrappers
var s = "my string";
var n = 101;
var b = true;
/*
only use primitive wrappers when you want to augment the value and persist state
*/
// primitive string
var greet = "Hello there";
// primitive is converted to an object
// in order to use the split() method
greet.split(' ')[0]; // "Hello"
// attemting to augment a primitive is not an error
greet.smile = true;
// but it doesn't actually work
console.log(typeof greet.smile); // "undefined"
// primitive wrapper
var greet = new String("Hello there");
// split() method is called directly on the object
greet.split(' ')[0]; // "Hello"
// augment the object
greet.smile = true;
// it works
console.log(typeof greet.smile); // "boolean"
// References
// http://shop.oreilly.com/product/9780596806767.do
</script>
</body>
</html>
Jump to Line
Something went wrong with that request. Please try again.