This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| var constant = (function () { | |
| var constants = {}, | |
| ownProp = Object.prototype.hasOwnProperty, | |
| allowed = { | |
| string: 1, | |
| number: 1, | |
| boolean: 1 | |
| }, | |
| prefix = (Math.random() + "_").slice(2); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| if (typeof Function.prototype.method !== "function") { | |
| Function.prototype.method = function (name, implementation) { | |
| this.prototype[name] = implementation; | |
| return this; // enable chaining function. | |
| }; | |
| } | |
| var Person = function (name) { | |
| this.name = name; | |
| }. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <!DOCTYPE html> | |
| <html> | |
| <head> | |
| <meta charset="UTF-8" /> | |
| <meta http-equiv="X-UA-Compatible" content="IE=edge"> | |
| <meta name="viewport" content="width=device-width, initial-scale=1"> | |
| <title>Lab</title> | |
| <link rel="shortcut icon" href="../favicon.ico"> | |
| </head> | |
| <body> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| function inherit(Child, Parent) { | |
| Child.prototype = new Parent(); //this is the prototype inhert magic! | |
| } | |
| var holyInherit = (function () { | |
| var F = function () {}; | |
| return function (C, P) { | |
| F.prototype = P.prototype; | |
| C.prototype = new F(); | |
| C.uper = P.prototype; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| var klass = function (Parent, props) { | |
| var Child, F, i; | |
| // 1. | |
| // new constructor | |
| Child = function () { | |
| if (Child.uber && Child.uber.hasOwnProperty("__construct")) { | |
| Child.uber.__construct.apply(this, arguments); | |
| } | |
| if (Child.prototype.hasOwnProperty("__construct")) { | |
| Child.prototype.__construct.apply(this, arguments); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| function extendDeep(parent, child) { | |
| var i, | |
| toStr = Object.prototype.toString, | |
| astr = "[object Array]"; | |
| child = child || {}; | |
| for (i in parent) { | |
| if (parent.hasOwnProperty(i)) { | |
| if (typeof parent[i] === "object") { | |
| child[i] = (toStr.call(parent[i]) === astr) ? [] : {}; | |
| extendDeep(parent[i], child[i]); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /* # bind() function - 3 impls that works | |
| * @ sample: | |
| * var newFunc = obj.someFunc.bind(myobj, 1, 2, 3); | |
| */ | |
| //pre ECMA5 bind function: | |
| // 《js Pattern》 version | |
| if (typeof Function.prototype.bind === "undefined") { | |
| Function.prototype.bind = function (thisArg) { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| //use ECMA5 selector api. | |
| document.querySelector("ul .selected"); | |
| document.querySelectorAll("#widget .class"); | |
| //use documentFragment to cache dom elements | |
| var p, t, frag; | |
| frag = document.createDocumentFragment(); | |
| p = document.createElement('p'); | |
| t = document.createTextNode('first paragraph'); | |
| p.appendChild(t); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| //change "Click me: + count" : count add 1 | |
| function myHandler(e) { | |
| var src, parts; | |
| // get event and source element | |
| e = e || window.event; | |
| src = e.target || e.srcElement; | |
| // actual work: update label | |
| parts = src.innerHTML.split(": "); | |
| parts[1] = parseInt(parts[1], 10) + 1; | |
| src.innerHTML = parts[0] + ": " + parts[1]; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <!-- html --> | |
| <button id="new">New game</button> | |
| <button id="server">Server play</button> | |
| <table> | |
| <tr> | |
| <td id="cell-1"> </td> | |
| <td id="cell-2"> </td> | |
| <td id="cell-3"> </td> | |
| </tr> | |
| <tr> |