Home > Web Front-end > JS Tutorial > body text

An introduction to some knowledge about literals and destructuring assignments

巴扎黑
Release: 2017-07-21 17:10:38
Original
1143 people have browsed it

Literal (independent variable)

     let name="wei";
    let age=3;
    let obj={
        //简写变量,等同于name:name
        name,
        age
    }
        console.log(obj.name)//wei
    let qqq = {
      name: 'wrs',
      toString () {  // 'function' keyword is omitted here
        return this.name;
      }
    };


    console.log(qqq.toString()); // wrs
Copy after login

//Create an object through an object literal

    var human = {
        breathe() {
            console.log('breathing...');
        }
    };
    var worker = {
        __proto__: human, //设置此对象的原型为human,相当于继承human
        company: 'freelancer',
        work() {
            console.log('working...');
        }
    };
    human.breathe();//输出 ‘breathing...’
    //调用继承来的breathe方法
    worker.breathe();//输出 ‘breathing...’
Copy after login

Destructuring assignment

Allows the extraction of values ​​from arrays and objects and assigns them to variable.
function foo() {
  return [1,2,3];
}
let arr = foo(); // [1,2,3]

let [a, b, c] = foo();
console.log(a, b, c); // 1 2 3

function bar() {
  return {
    x: 4,
    y: 5,
    z: 6
  };
}
let {x: x, y: y, z: z} = bar();
console.log(x, y, z); // 4 5 6
Copy after login

The above is the detailed content of An introduction to some knowledge about literals and destructuring assignments. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!