龙空技术网

JavaScript基础拓展4项

龙骑士洞察 277

前言:

现在看官们对“js四”都比较关心,姐妹们都想要学习一些“js四”的相关资讯。那么小编同时在网上汇集了一些关于“js四””的相关文章,希望咱们能喜欢,你们快快来了解一下吧!

一、传值还是传参

primitive-type的数据则pas-by-value,而object-type的数据则pass-by-reference。

二、Functions的种类

// 匿名函数const awesomeFunction = function(coolThings) {        // ..        return amazingStuff;    };    // arrow function expressions    var f;    f = () => 42;    f = x => x * 2;    f = (x) => x * 2;    f = (x,y) => x * y;    f = x => ({ x: x * 2 });    f = x => { return x * 2; };    f = async x => {        var y = await doSomethingAsync(x);        return y * 2;    };    someOperation( x => x * 2 );    // ..    // generator function declaration    function *two() { .. }    // async function declaration    async function three() { .. }    // async generator function declaration    async function *four() { .. }    // named function export declaration (ES6 modules)    export function five() { .. }
三、Coercive Conditional Comparison

accurate mental model

    var x = "hello";    if (x) {        // will run!    }    if (x == true) {        // won't run :(    }

准确的accuate-model为:

    var x = "hello";    if (Boolean(x) == true) {        // will run    }    // which is the same as:    if (Boolean(x) === true) {        // will run    }
四、Prototypal Classes

尝试用object构建。

    var Classroom = {        welcome() {            console.log("Welcome, students!");        }    };    var mathClass = Object.create(Classroom);    mathClass.welcome();    // Welcome, students!

prototypal-classes

    function Classroom() {        // ..    }    Classroom.prototype.welcome = function hello() {        console.log("Welcome, students!");    };    var mathClass = new Classroom();    mathClass.welcome();    // Welcome, students!

当前prototypal-class已被淘汰,取而代之以ES6's class:

    class Classroom {        constructor() {            // ..        }        welcome() {            console.log("Welcome, students!");        }    }    var mathClass = new Classroom();    mathClass.welcome();    // Welcome, students!
五、总结

JS是以object为基石。

标签: #js四