友链
导航
These are the good times in your life,
so put on a smile and it'll be alright
友链
导航
// 在 ES5 中,如果要 require babel6 解析 ES2015 生成的模块,则需: var foo = require('./foo.js'); if (foo.default) { foo = foo.default; }
import fetch from '../core/fetch'; export const path = '/products'; export const action = async () => { const response = await fetch('/graphql?query={products{id,name}}'); const data = await response.json(); return <Layout><Products {...data} /></Layout>; };
es6 中包含很多类似 python 的用法.
let
, const
for block scopingfor (p of planets)
let [a, b] = ['foo', 'bar']; let {date: d, month: m} = {date: 1, month: 4};
[(console.log(s + “ with a ” + w + “ in the ” + r)) for (s of suspects) for (w of weapons) for (r of rooms)];
class
, with extends, prototype, and super:Set
& Map
(Map 可用任意类型作为 key)function foo(bar = 'baz') {}
function push(array, …items){}; push(planets, “Mercury”, “Venus”, “Earth”, “Mars”);
function createURL (comment, path, protocol, subdomain, domain, tld) {}; createURL('haha', ['haha.html', 'http', #省略]);
`You are ${age} years old.`
, 在字符串里用变量(更像 php)module math { export function sum(x, y) { return x + y; } export var pi = 3.141593; } import {sum, pi} from math; alert(sum(pi,pi));
node –harmony
, 可使用 Set, Map, 等// 顺序无关时的数组遍历 var foo = [1, 2, 3]; for (var i = foo.length; i--; ) { console.log(foo[i]); }
Javascript模块化编程(一):模块的写法 - 阮一峰的网络日志
// 使用"立即执行函数"(Immediately-Invoked Function Expression,IIFE), // 可以达到不暴露私有成员的目的。 var module = (function(){ var _count = 0; var m1 = function(){ //... }; var m2 = function(){ //... }; return { m1 : m1, m2 : m2 }; })(); // 如果一个模块很大,必须分成几个部分,或者一个模块需要继承另一个模块, // 这时就有必要采用"放大模式"(augmentation)。 var module = (function (mod){ mod.m3 = function () { //... }; return mod; })(module); // 独立性是模块的重要特点,模块内部最好不与程序的其他部分直接交互。 // 为了在模块内部调用全局变量,必须显式地将其他变量输入模块。 var module = (function ($, YAHOO) { //... })(jQuery, YAHOO);
function Car( model, year, miles ) { this.model = model; this.year = year; this.miles = miles; } // Note here that we are using Object.prototype.newMethod rather than // Object.prototype so as to avoid redefining the prototype object Car.prototype.toString = function () { return this.model + " has done " + this.miles + " miles"; }; // Usage: var civic = new Car( "Honda Civic", 2009, 20000 ); var mondeo = new Car( "Ford Mondeo", 2010, 5000 ); console.log( civic.toString() ); console.log( mondeo.toString() );
var wait = function(){ var dtd = $.Deferred(); //在函数内部,新建一个Deferred对象 var tasks = function(){ alert("执行完毕!"); dtd.resolve(); // 改变Deferred对象的执行状态 }; setTimeout(tasks,5000); return dtd.promise(); // 返回promise对象 }; $.when(wait()) .done(function(){ alert("哈哈,成功了!"); }) .fail(function(){ alert("出错啦!"); });
'$.when($.ajax( “/main.php” )).then(successFunc[, failureFunc] );
'把接受多个参数的函数变换成接受一个单一参数(最初函数的第一个参数)的函数,并且返回接受余下的参数而且返回结果的新函数的技术
In mathematics and computer science, currying is the technique of translating the evaluation of a function that takes multiple arguments (or a tuple of arguments) into evaluating a sequence of functions, each with a single argument (partial application). It was introduced by Moses Schönfinkel and later developed by Haskell Curry.
a curried add()
// accepts partial list of arguments function add(x, y) { var oldx = x, oldy = y; if (typeof oldy === "undefined") { // partial return function (newy) { return oldx + newy; }; } // full application return x + y; } // test typeof add(5); // "function" add(3)(4); // 7 // create and store a new function var add2000 = add(2000); add2000(10); // 2010
Here is the general-purpose currying function:
// Here is the general-purpose currying function: function schonfinkelize(fn) { var slice = Array.prototype.slice, stored_args = slice.call(arguments, 1); return function () { var new_args = slice.call(arguments), args = stored_args.concat(new_args); return fn.apply(null, args); }; } // a normal function function add(x, y) { return x + y; } // curry a function to get a new function var newadd = schonfinkelize(add, 5); newadd(4); // 9 // another option -- call the new function directly schonfinkelize(add, 6)(7); // 13 // The transformation function schonfinkelize() is not limited to // single parameters or to single-step currying. Here are some more // usage examples: // a normal function function add(a, b, c, d, e) { return a + b + c + d + e; } // works with any number of arguments schonfinkelize(add, 1, 2, 3)(5, 5); // 16 // two-step currying var addOne = schonfinkelize(add, 1); addOne(10, 10, 10, 10); // 41 var addSix = schonfinkelize(addOne, 2, 3); addSix(5, 5); // 16