JavaScript函数笔记by-Lying

JavaScript函数笔记by-Lying

本笔记资源来自廖雪峰的官方网站

  • 定义函数的两种方式
function abs(x) {
    if (x >= 0) {
        return x;
    } else {
        return -x;
    }
}
var abs = function (x) {
    if (x >= 0) {
        return x;
    } else {
        return -x;
    }
};   //要加分号
  • 可以传入任意个参数,不会影响调用;
abs(10, 'blablabla'); // 返回10
abs(-9, 'haha', 'hehe', null); // 返回9
abs(); // 返回NaN
//此时abs(x)函数的参数x将收到undefined,计算结果为NaN。

//要避免收到undefined,可以对参数进行检查
function abs(x) {
    if (typeof x !== 'number') {
        throw 'Not a number';
    }
    if (x >= 0) {
        return x;
    } else {
        return -x;
    }
}

arguments

  • JavaScript在函数内部有一个关键字arguments,永远指向当前函数的调用者传入的所有参数(类似array但不是array),最常用于判断传入参数的个数;

  • 可通过arguments判断,将某参数设为可选参数;

// foo(a[, b], c)
// 接收2~3个参数,b是可选参数,如果只传2个参数,b默认为null:
function foo(a, b, c) {
    if (arguments.length === 2) {
        // 实际拿到的参数是a和b,c为undefined
        c = b; // 把b赋给c
        b = null; // b变为默认值
    }
    // ...
}

rest

  • ES6标准引入rest参数;

  • rest只能写在最后,前面用…标识,传入的参数先绑定前面定义的参数,多余的参数以数组形式传给变量rest;

  • 如果传入的参数没填满前面定义的参数,rest参数会接收到一个空数组(不是undefined)。

function foo(a, b, ...rest) {
    console.log('a = ' + a);
    console.log('b = ' + b);
    console.log(rest);
}

foo(1, 2, 3, 4, 5);
// 结果:
// a = 1
// b = 2
// Array [ 3, 4, 5 ]

foo(1);
// 结果:
// a = 1
// b = undefined
// Array []
  • JavaScript的嵌套函数中,如果内部函数定义了与外部函数重名的变量,则内部函数的变量将“屏蔽”外部函数的变量。
Paperback: 280 pages Publisher: Packt Publishing - ebooks Account (February 1, 2016) Language: English ISBN-10: 1785280082 ISBN-13: 978-1785280085 Key Features Learn to use the facilities provided by D3.js to create data-driven visualizations Explore the concepts of D3.js through examples that enable you to quickly create visualizations including charts, network diagrams, and maps Get practical examples of visualizations using real-world data sets that show you how to use D3.js to visualize and interact with information to glean its underlying meaning Book Description This book will take you through all the concepts of D3.js starting with the most basic ones and progressively building on them in each chapter to expand your knowledge of D3.js. Starting with obtaining D3.js and creating simple data bindings to non-graphical HTML elements, you will then master the creation of graphical elements from data. You'll discover how to combine those elements into simple visualizations such as bar, line, and scatter charts, as well as more elaborate visualizations such as network diagrams, Sankey diagrams, maps, and choreopleths. Using practical examples provided, you will quickly get to grips with the features of D3.js and use this learning to create your own spectacular data visualizations with D3.js. What you will learn Install and use D3.js to create HTML elements within the document Use development tools such as JSBIN and Chrome Developer Tools to create D3.js applications Retrieve JSON data and use D3.js selections and data binding to create visual elements from data Create and style graphical elements such as circles, ellipses, rectangles, lines, paths, and text using SVG Turn your data into bar and scatter charts, and add margins, axes, labels, and legends Use D3.js generators to perform the magic of creating complex visualizations from data Add interactivity to your visualizations, including tool-tips, sorting, hover-to-highlight, and grouping and dragging of visuals
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值