javascript --- > 编程风格

字符串

const a = 'foobar';
const b = `foo${a}bar`;   // 此处是反引号(tab键上)
const c = 'foobar';

解构赋值

const [first, second] = arr;

function getFullName({ firstName, lastName }) {
}

function processInput(input) {
    return { left, right, top, bottom };
}
const { left, right } = processInput(input);

对象

const a = { k1: v1, k2: v2};   // 注意逗号
const b = {
    k1: v1,
    k2: v2,    // 注意逗号
}

const obj = {
    id: 5,
    name: 'San Francisco',
    [getKey('enabled')]: true,
};

const atom = {
    ref,
    
    value: 1,
     
    addValue(value) {
        return atom.value + value;
    },
};  

数组

const itemsCopy = [...items];

const foo = document.querySelectorAll('.foo');
const nodes = Array.from(foo);

函数

//  立即执行函数可以写成箭头函数的形式
( () => {
    console.log('Welcome to the Internet.');
}) ();

[1, 2, 3].map(x => x * x) ;

const boundMethod = (...params) => method.apply(this, params);

function divide(a, b, { option = false } = {}) {}

function concatenateAll(...args) {
    return args.jopin(' ');
}

function handlerThings(opts = {}) {
    // ....
}

Map结构

let map = new Map(arr);

for (let key of map.keys()) {
    console.log(key);
}

for (let value of map.values()) {
    console.log(value);
}

for (let item of mao.entries()) {
    console.log(item[0], item[1]);
}

Class

class Queue {
    constructor(contents = []) {
        this._queue = [...contents];
    }
    pop () {
        const value = this._queue[0];
        this._queue.splice(0, 1);
        return value;
    }
}

class PeekableQueue extends Queue {
    peek() {
        return this._queue[0];
    }
}

模块

import { func1, func2 } from 'moduleA';

// 使用export取代module.exports

// commonJS的写法
var React = require('react);

var Breadcrumbs = React.createClass({
    render() {
        return <nav />;
    }
});
module.exports = Breadcrumbs;

// ES6的写法
import React from 'react';

class Breadcrumbs extends React.Component {
    render() {
        return <nav />;
    }
};

export default Breadcrumbs
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值