Development easier to understand the code routines

Foreword

Today to sort out handmade I think the more useful ideas in the development of the code routines, welcome to discuss with me

Programming Chain

The so-called chain that is programmed to return after the function call itself the object

var calculator = {
    total:0,
    add (n) {
        // 模拟执行功能
		this.total += n; // 为了能在调用完add()后继续.substruct() 因此我们返回对象本身 return this; }, subtract(n) { this.total -= n; return this; } } // 调用 calculator.add(5).add(5).subtract(2).total; // 8 

Application of higher-order functions - (add data and provides deletion method)

For chestnut: We have some business needs to add elements to the array, but it also might be too have to be removed. A better approach would we put them into a package function, the use of higher-order functions and closures characteristics marked for deletion, thus reducing the problem to find this element of the

var students = [];
function addStudent (stu) { // 加入 通过形参标记该学员 students.push(stu); return function () { // 利用闭包获取stu var index = students.indexOf(stu); // 删除该学生 students.splice(index); } } var stu = { name:'小明' }; // 试用一下 var stu1 = addStudent(stu); var stu2 = addStudent({name:'小红'}); // 一年想到需要删除了,我们不需要查找他们了 stu1(); // 删除小明 stu2(); // 删除小红 

Promise using cutting chain (delayed execution)

Promise not only help to make asynchronous process control, process control step while what can do, of course, is more important and asynchronous delay in order to ensure, for example step A to step B, and the like among us to also determine the step When are completed, it is possible to define two good steps to save the resolve a step up when you need to call waiting call can (and do not try to compare the callback function, because Promise itself is not elegant to solve the problem callback function)

// 任务A
function task1() { setTimeout(function(){ // 第一件事 },2000); } function task2() { setTimeout(function(){ // 第二件事 },1000); } // 我先让第一件事执行,并保存其实例和resolve var token = (function(){ var next; var p = new Promise(function(resolve){ // 获取放行的钥匙 next = resolve; task1(); }); return { promise:p, next:next } })(); // 定义第二件事 token.promise.then(task2); // 想让第二件事执行的话,看我心情咯,来个定时器吧 setTimeout(function () { // 就现在吧,做第二件事吧(延迟执行) token.next(); },Math.random() * 1000 ); 

Problem is solved by an intermediate layer (decorator pattern)

JS is not saying any problems can be solved by an intermediate layer, for example, we wrote a dinner function, suddenly remembered wash hands before eating, then how to do it? Intermediate layer you think I wrote to contrast the middle layer, think about what is the difference? !

var obj = {
    eat:function () { console.log('我愉快的吃饭..'); } } obj.wash = function () { console.log('洗手'); } // 装饰者模式 obj.plus = function (fn1,fn2) { fn1(); return fn2(); } // 突然学习到《《《饭前要洗手》》》,怎么办? obj.plus(obj.wash,obj.eat); 

Singleton

Sometimes we need to have a globally unique instance, instead of once a new time, use the code below it, but pay attention to the following details

var Leader = (function() {
    var instance; // 闭包不让外部访问 return function (name) {// 构造函数 if (instance) return instance; instance = this; this.name = name; } })(); new Leader('凃老师'); // { name:'凃老师'} new Leader('启道学院'); // { name:'凃老师'}
来源:站长平台

Guess you like

Origin www.cnblogs.com/1994july/p/12412026.html