Advantage Avoid re-created instance method to this inside of the constructor. method() implementation The method() takes two parameters: • The name of the new method • The implementation of the method if (typeof Function.prototype.method !== "functio…
Scenario You want to use just the methods you like, without inheriting all the other methods that you’ll never need. This is possible with the borrowing methods pattern, which benefits from the function methods call() and apply(). // call() example…
Shallow copy pattern function extend(parent, child) { var i; child = child || {}; for (i in parent) { if (parent.hasOwnProperty(i)) { child[i] = parent[i]; } } return child; } Deep copy pattern function extendDeep(parent, child) { var i, toStr = Obje…
No classes involved; Objects inherit from other objects. Use an empty temporary constructor function F(). Set the prototype of F() to be the parent object. Return a new instance of the temporary constructor. function Object(o) { function F() {} F.…
Commonalities • There’s a convention on how to name a method, which is to be considered the constructor of the class. • Classes inherit from other classes. • There’s access to the parent class (superclass) from within the child class. The function ta…
Chaining Pattern - Call methods on an object one after the other without assigning the return values of the previous operations to variables and without having to split your calls on multiple lines. var obj = { value: 1, increment: function () { this…
Public Static Members // constructor var Gadget = function (price) { this.price = price; }; // a static method Gadget.isShiny = function () { // this always works var msg = "you bet"; // Checking if the static method is called by instance. if (t…
All object members are public in JavaScript. var myobj = { myprop : 1, getProp : function() { return this.myprop; } }; console.log(myobj.myprop); // `myprop` is publicly accessible console.log(myobj.getProp()); // getProp() is public too The same is…
Function Application apply() takes two parameters: the first one is an object to bind to this inside of the function, the second is an array or arguments, which then becomes the array-like arguments object available inside the function. If the first…
7.1 Singleton The idea of the singleton pattern is to have only one instance of a specific class. This means that the second time you use the same class to create a new object, you should get the same object that was created the first time. var obj =…
Loop through arguments and copy every property of every object passed to the function. And the result will be a new object that has the properties of all the source objects. function mix() { var arg, prop, child = {}; for (arg = 0; arg < arguments.le…
In Java you could do something like: Person adam = new Person(); In JavaScript you would do: var adam = new Person(); JavaScript’s constructor invocation looks as if Person were a class, but it’s important to keep in mind that Person is still just a…
Principle Make variables shouldn't be changed stand out using all caps. Add constants as static properties to the constructor function. // constructor var Widget = function () { // implementation... }; // constants Widget.MAX_HEIGHT = 320; Widget.MAX…
Drawbacks of the namespacing pattern • Reliance on a single global variable to be the application’s global. In the namespacing pattern, there is no way to have two versions of the same application or library run on the same page, because they both ne…
It’s a good idea to declare the modules your code relies on at the top of your function or module. The declaration involves creating only a local variable and pointing to the desired module. var myFunction = function() { // dependencies var event = Y…
Configuration Objects Passing a large number of parameters is not convenient. A better approach is to substitute all the parameters with only one and make it an object. var conf = { username: "batman", first: "Bruce", last: "Wayne…
Gets a length property containing the number of arguments the function expects: function func(a, b, c) {} console.log(func.length); var myFunc = function () { // serialize the arguments object as a JSON string and use that string as a key in your cac…
When you know that a certain condition will not change throughout the life of the program, it makes sense to test the condition only once. Browser sniffing (or feature detection) is a typical example. // BEFORE var utils = { addListener : function(el…
java.lang.RuntimeException: java.lang.Throwable: A WebView method was called on thread 'JavaBridge'. All WebView methods must be called on the same thread 之前的 android4.4版本的代码跑没有问题,到了5.0出了这个问题.查询百度.解决方法: //4.4之前版本调用函数 public void sendToJs(){ try { M…
Like an array, Observable has a map method that allows us to transform a sequence into a new Observable. var Observable = Rx.Observable; //Create click events by Observable var clicks = Observable.fromEvent(button, 'click'); var points = clicks.map(f…
In addition to flat Arrays, programmers must often deal with nested Arrays. For example let's say we have an Array of stock exchanges, each of which is represented by an array of all the stocks listed on that exchange. If we were looking for a stock…
JSON: JavaScript Object Notation {"name": "value", "some": [1, 2, 3]} The only syntax difference between JSON and the object literal is that property names need to be wrapped in quotes to be valid JSON. In object literals th…
Free and open source tools for doc generation: the JSDoc Toolkit (http://code.google.com/p/jsdoc-toolkit/) and YUIDoc (http://yuilibrary.com/projects/yuidoc). Process of generating API documentation • Writing specially formatted code blocks • Running…