[ Web Proxy ]
URL:
Viewing: https://raw.githubusercontent.com/TooBug/javascript.patterns/master/chapter3.markdown [Back]  [Original]

# JavaScript`Object()``Array()`JSONJSON`new` `Number()``String()``Boolean()``Error()` ## JavaScripthash tablepropertymethod JavaScriptobject literal notation // var dog = {}; // dog.name = "Benji"; // dog.getName = function () { return dog.name; }; - dog.getName = function () { // return "Fido"; }; - / delete dog.name; - dog.say = function () { return "Woof!"; }; dog.fleas = true; var dog = { name: "Benji", getName: function () { return this.name; } }; > blank objectempty objectJavaScript`{}``Object.prototype`empty(own properties) ### - `{` `}` - IE - - `}` ### JavaScriptJavaScriptJavaScriptJava `Object()``Date()``String()` // var car = {goes: "far"}; // // var car = new Object(); car.goes = "far"; `Object()`scope resolution`Object()``Object()``Object()` ### Object() > Object Constructor Catch `new Object()``Object()` `new Object()` // // var o = new Object(); console.log(o.constructor === Object); // true // var o = new Object(1); console.log(o.constructor === Number); // true console.log(o.toFixed(2)); // "1.00" // var o = new Object("I am a string"); console.log(o.constructor === String); // true // substring() console.log(typeof o.substring); // "function" // var o = new Object(true); console.log(o.constructor === Boolean); // true `Object()``new Object()` ## var adam = new Person("Adam"); adam.say(); // "I am Adam" Java`Person`JavaScript`Person()` `Person()` var Person = function (name) { this.name = name; this.say = function () { return "I am " + this.name; }; }; `new` - `this` - `this` - this var Person = function (name) { // // var this = {}; // this.name = name; this.say = function () { return "I am " + this.name; }; //return this; }; `say()``this``new Person()``say()``say()``Person()` Person.prototype.say = function () { return "I am " + this.name; }; `this` // var this = {}; `Person` // var this = Object.create(Person.prototype); `Object.create()` ### `new``this``this` `return``this``that` var Objectmaker = function () { // name this.name = "This is it"; // var that = {}; that.name = "And that's that"; return that; }; // var o = new Objectmaker(); console.log(o.name); // "And that's that" `this` ## new `new``new``new``new``this``this``window` `this.member``new``member``window.member``member` // function Waffle() { this.tastes = "yummy"; } // var good_morning = new Waffle(); console.log(typeof good_morning); // "object" console.log(good_morning.tastes); // "yummy" // new var good_morning = Waffle(); console.log(typeof good_morning); // "undefined" console.log(window.tastes); // "yummy" ECMAScript5`this`ES5JavaScript`new` ### `MyConstructor()``myFunction` ### that `this``that``that` function Waffle() { var that = {}; that.tastes = "yummy"; return that; } `that` function Waffle() { return { tastes: "yummy" }; } `new` var first = new Waffle(), second = Waffle(); console.log(first.tastes); // "yummy" console.log(second.tastes); // "yummy" `Waffle()` > `that``that``self``me` ### `this``new` function Waffle() { if (!(this instanceof Waffle)) { return new Waffle(); } this.tastes = "yummy"; } Waffle.prototype.wantAnother = true; // var first = new Waffle(), second = Waffle(); console.log(first.tastes); // "yummy" console.log(second.tastes); // "yummy" console.log(first.wantAnother); // true console.log(second.wantAnother); // true `arguments.callee` if (!(this instanceof arguments.callee)) { return new arguments.callee(); } `arguments``arguments``callee`ES5`arguments.callee` ## JavaScript`Array()` `Array()` // // var a = new Array("itsy", "bitsy", "spider"); // var a = ["itsy", "bitsy", "spider"]; console.log(typeof a); // "object" console.log(a.constructor === Array); // true ### 0`new` ### Array() `new Array()` `Array()``new Array(3)`33`undefined` // 1 var a = [3]; console.log(a.length); // 1 console.log(a[0]); // 3 // 3 var a = new Array(3); console.log(a.length); // 3 console.log(typeof a[0]); // "undefined" `new Array()`new Array()RangError // var a = [3.14]; console.log(a[0]); // 3.14 var a = new Array(3.14); // RangeError: invalid array length console.log(typeof a); // "undefined" `new Array()` > `Array()`255256`var white = new Array(256).join(' ');` ### `typeof`object console.log(typeof [1, 2]); // "object" `length``slice()``instanceof Array`IEiframeiframe`prototype` ECMAScript5`Array.isArray()`true Array.isArray([]); // true // Array.isArray({ length: 1, "0": 1, slice: function () {} }); // false ECMAScript5`Object.prototype.toString()``toString``call()`[object Array][object Object] if (typeof Array.isArray === "undefined") { Array.isArray = function (arg) { return Object.prototype.toString.call(arg) === "[object Array]"; }; } ## JSON JSONJSONJavaScript Object NotationJavaScript JSONJSON {"name": "value", "some": [1, 2, 3]} JSONJSON`{"first name": "Dave"}` JSON ### JSON `eval()`JSON`JSON.parse()`ES5JavaScriptJSONJavaScriptJSON.orgJSJSON // JSON var jstr = '{"mykey": "my value"}'; // var data = eval('(' + jstr + ')'); // var data = JSON.parse(jstr); console.log(data.mykey); // "my value" JavaScriptJSONJSON.orgYUI3 // JSON var jstr = '{"mykey": "my value"}'; // YUI YUI().use('json-parse', function (Y) { var data = Y.JSON.parse(jstr); console.log(data.mykey); // "my value" }); jQuery`parseJSON()` // JSON var jstr = '{"mykey": "my value"}'; var data = jQuery.parseJSON(jstr); console.log(data.mykey); // "my value" `JSON.parse()``JSON.stringify()`JSON var dog = { name: "Fido", dob:new Date(), legs:[1,2,3,4] }; var jsonstr = JSON.stringify(dog); // jsonstr // {"name":"Fido","dob":"2010-04-11T22:36:22.436Z","legs":[1,2,3,4]} ## JavaScript - `new RegExp()` - \ // var re = /\\/gm; // var re = new RegExp("\\\\", "gm"); `RegExp()` ### JavaScript - `g` - `m` - `i` var re = /pattern/gmi; `String.prototype.replace()` var no_letters = "abc123XYZ".replace(/[a-z]/gi, ""); console.log(no_letters); // 123 `new RegExp()` `lastIndex` function getRE() { var re = /[a-z]/; re.foo = "bar"; return re; } var reg = getRE(), re2 = getRE(); console.log(reg === re2); // true reg.foo = "baz"; console.log(re2.foo); // "baz" > ECMAScript5 `new``RegExp()``new``RegExp()` ## JavaScript`null``undefined``null``undefined`primitive wrapper object`Number()``String()``Boolean()` // var n = 100; console.log(typeof n); // "number" // Number var nobj = new Number(100); console.log(typeof nobj); // "object" `toFixed()``toExponential()``substring()``chatAt()``toLowerCase()``length` // var s = "hello"; console.log(s.toUpperCase()); // "HELLO" // "monkey".slice(3, 6); // "key" // (22 / 7).toPrecision(3); // "3.14" new String("hi")"hi" // var s = new String("my string"); var n = new Number(101); var b = new Boolean(true); // var s = "my string"; var n = 101; var b = true; // var greet = "Hello there"; // split greet.split(' ')[0]; // "Hello" // greet.smile = true; // typeof greet.smile; // "undefined" `greet``greet``new String()``smile` `new` typeof Number(1); // "number" typeof Number("1"); // "number" typeof Number(new Number()); // "number" typeof String(1); // "string" typeof Boolean(1); // "boolean" ## JavaScript`Error()``SyntaxError()``TypeError()``throw` - `name` nameErrorRangeError - `message` `throw`namemessage`catch` try { // throw { name: "MyErrorType", // message: "oops", extra: "This was rather embarrassing", remedy: genericErrorHandler // }; } catch (e) { // alert(e.message); // "oops" // e.remedy(); // genericErrorHandler() } `new``new` ## - - - `new``new` - - JSON - - `String()``Number()``Boolean()``Error()` `Date()`
var o = new Object(); var o = {};
var a = new Array(); var a = [];
var re = new RegExp("[a-z]","g"); var re = /[a-z]/g;
var s = new String(); var s = "";
var n = new Number(); var n = 0;
var b = new Boolean(); var b = false;
throw new Error("uh-oh"); throw { name: "Error",message: "uh-oh"};throw Error("uh-oh");

Web Proxy Viewer  |  New URL  |  Original Page