美文网首页
js编程的一些小技巧

js编程的一些小技巧

作者: Mcq | 来源:发表于2019-12-07 22:42 被阅读0次

1.用闭包保护对象属性

The simplest way to make this public property private is by creating a variable within the constructor function. 
function Bird() {
  let weight = 15
  this.getWeight = function () {
    return weight
  }
}

  1. 函数式编程:了解命令式代码的危害

典型的命令式代码就是for循环,它明确的指定操作数组的下标。(每件事情都请力亲为)
相反的,函数式编程通过函数告诉计算机你希望的操作,而不必每次都思考走路的时候先迈哪条腿。比如与for对应的map,filter相关操作。
这有助于避免语义错误,比如调试一节中提到的“Off By One error”。

example

// tabs is an array of titles of each site open within the window
var Window = function(tabs) {
  this.tabs = tabs; // we keep a record of the array inside the object
};

// When you join two windows into one window
Window.prototype.join = function (otherWindow) {
  this.tabs = this.tabs.concat(otherWindow.tabs);
  return this;
};

// When you open a new tab at the end
Window.prototype.tabOpen = function (tab) {
  this.tabs.push('new tab'); // let's open a new tab for now
  return this;
};

// When you close a tab
Window.prototype.tabClose = function (index) {

  // Only change code below this line

  var tabsBeforeIndex = this.tabs.slice(0, index); // get the tabs before the tab
  var tabsAfterIndex = this.tabs.slice(index + 1); // get the tabs after the tab

  this.tabs = tabsBeforeIndex.concat(tabsAfterIndex); // join them together

  // Only change code above this line

  return this;
 };

// Let's create three browser windows
var workWindow = new Window(['GMail', 'Inbox', 'Work mail', 'Docs', 'freeCodeCamp']); // Your mailbox, drive, and other work sites
var socialWindow = new Window(['FB', 'Gitter', 'Reddit', 'Twitter', 'Medium']); // Social sites
var videoWindow = new Window(['Netflix', 'YouTube', 'Vimeo', 'Vine']); //  Entertainment sites

// Now perform the tab opening, closing, and other operations
var finalTabs = socialWindow
                    .tabOpen('new tab') // Open a new tab for cat memes
                    .join(videoWindow.tabClose(2)) // Close third tab in video window, and join
                    .join(workWindow.tabClose(1).tabOpen('new tab'));
console.log(finalTabs.tabs)

3.用函数式编程避免副作用

函数式编程的核心原则之一是不更改值。
改变函数参数和全局变量被称为突变,其结果被称为副作用。
以传参的方式避免函数内部对外依赖

相关文章

  • js编程的一些小技巧

    1.用闭包保护对象属性 函数式编程:了解命令式代码的危害 典型的命令式代码就是for循环,它明确的指定操作数组的下...

  • js 一些小技巧

    for(leti=0,len=_map.length;i

  • js编程常用技巧合集

    js编程常用技巧合集 最近看了一些JavaScript的编程技巧,个人觉得特别有用所以把我觉得好的分享给大家。 1...

  • js 编程技巧

    本篇用于记录日常编程技巧,以供日后查询Created by RuiLin2017/12/10 jQuery 遍历对...

  • 安卓编程技巧总结(6) APP安全分析

    allowbackup设置 数据安全 intent校验 网络加密 webview js交互 第一篇: 安卓编程技巧...

  • 写js代码的一些小技巧

    一.变量 使用有意义的且常用的单词命名 常量全大写: 避免无意义的命名: 传参使用默认值: 二.函数 函数参数最好...

  • 《高性能JavaScript》读书笔记

    零、组织结构根据引言,作者将全书划分为四个部分:一、页面加载js的最佳方式(开发前准备)二、改善js代码的编程技巧...

  • 前后端代码样式轮子

    HTML编程技巧-达达 1 解决滚动条影响布局 2 百度富文本编辑器 ①js代码引入 ②html代码引入 ③JS控...

  • 编程小记录

    简介 本文主要记录一些平常编程中的一些小技巧[NB:该文章为持续更新型] 没想好标题 if(nullptr==p)...

  • 现代js编程小技巧

    我的博客主页:笔头博客 1、Array.includes ES6提供,用于判断数组中是否包含指定值,返回 true...

网友评论

      本文标题:js编程的一些小技巧

      本文链接:https://www.haomeiwen.com/subject/qzhpgctx.html