JavaScript基础拓展4项

一、传值还是传参

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为基石。

展开阅读全文

页面更新:2024-05-06

标签:基石   取而代之   函数   种类   准确   基础   数据   科技

1 2 3 4 5

上滑加载更多 ↓
推荐阅读:
友情链接:
更多:

本站资料均由网友自行发布提供,仅用于学习交流。如有版权问题,请与我联系,QQ:4156828  

© CopyRight 2020-2024 All Rights Reserved. Powered By 71396.com 闽ICP备11008920号-4
闽公网安备35020302034903号

Top