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 MyArray() {} | |
| //MyArray.prototype = new Array(); //subclass Array, which is not compatible across all browsers. | |
| MyArray.prototype.length = 0; | |
| (function () { | |
| var methods = ['push', 'pop', 'shift', 'unshift', | |
| 'slice', 'splice', 'join']; | |
| for (var i = 0; i < methods.length; i++)(function (name) { | |
| MyArray.prototype[name] = function () { | |
| return Array.prototype[name].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 () { | |
| var initializing = false, | |
| superPattern = | |
| /xyz/.test(function () { //check if supoort fucntion serialization. | |
| xyz; | |
| }) ? /\b_super\b/ : /.*/; | |
| Object.subClass = function (properties) { | |
| var _super = this.prototype; | |
| initializing = true; |
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
| <div class="samurai ninja"></div> | |
| <div class="ninja samurai"></div> | |
| <div></div> | |
| <span class="samurai ninja ronin"></span> | |
| <script> | |
| function findClassInElements(className, type) { | |
| var elems = document.getElementsByTagName(type || "*"); | |
| var regex = new RegExp("(^|\\s)" + className + "(\\s|$)"); // match either the beginning of a string or whitespace; adn class name; and the end of string... | |
| var results = []; |
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
| //// warm-ups | |
| //var str = 'opacity=300'; | |
| //var reg = /opacity=([\d]{1,2})/; | |
| //var res = (parseFloat(str.match(reg)[1]) / 100) + ""; | |
| //html tag match | |
| var res = []; | |
| var html = "<div class='test'><b>Hello</b> <i>world!</i></div>"; | |
| var tag = /<(\/?)(\w+)([^>]*?)>/g, | |
| match; |
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 compress(source) { | |
| var keys = {}; | |
| source.replace( | |
| /([^=&]+)=([^&]*)/g, | |
| function (full, key, value) { | |
| keys[key] = (keys[key] ? keys[key] + "," : "") + value; | |
| console.log(keys[key]); | |
| return ""; |
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
| String.prototype.trim = function () { | |
| var reg = /^\s+|\s+$/g, | |
| tr = function (full, hs, content, es) { | |
| alert('full: ' + full + '; hs: ' + hs + '; content: ' + content + '; es: ' + es); | |
| return content; | |
| }; | |
| return this.replace(reg, ''); | |
| }; | |
| //trim with string functions, with very poor performance. |
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
| <body> < div id = "box" style = "position:relative;display:inline;" > Bird! </div> | |
| </body> | |
| <script> | |
| var timers = { | |
| timerID: 0, | |
| timers: [], | |
| add: function (fn) { | |
| this.timers.push(fn); | |
| }, | |
| start: function () { |
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 Object.getPrototypeOf === "undefined") { | |
| Object.getPrototypeOf = function (obj) { | |
| var t = typeof obj; | |
| if (!obj || (t !== "object" && t !== "function")) { | |
| throw new TypeError("not an object"); | |
| } | |
| return obj.__proto__; | |
| }; | |
| } |
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 Object.create === "undefined") { | |
| Object.create = function (prototype) { | |
| function C() {} | |
| C.prototype = prototype; | |
| return new C(); | |
| }; | |
| } | |
| function User(name, passwordHash) { | |
| var self = this instanceof User ? this : Object.create(User.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
| function loadScript(url, callback) { | |
| var script = document.createElement("script"); | |
| script.type = "text/javascript"; | |
| if (script.readyState) { //IE | |
| script.onreadystatechange = function () { | |
| if (script.readyState == "loaded" || script.readyState == "complete") { | |
| script.onreadystatechange = null; | |
| callback(); | |
| } | |
| }; |