javascript ES6类型和对象扩展

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了javascript ES6类型和对象扩展相关的知识,希望对你有一定的参考价值。

let buffer = new ArrayBuffer(1024);
console.log(buffer.byteLength);
let salaries = Array.of(9000);
console.log(salaries);

const amounts = [800, 810, 820]
salaries = Array.from(amounts, v => v + 100);
console.log(salaries);

// Start filling in pos 1 and 2 pos to stop at
salaries.fill(900, 1, 2);
console.log(salaries);

salaries = amounts
let result = salaries.find(value => value >= 750);
console.log(result); // returns first value found in the criteria

salaries = amounts
salaries.copyWithin(2, 0); // 2 = where to copy, 0 = item to copy
console.log(salaries);

let ids = ['A', 'B', 'C'];
// Prints out [ 0, 'A' ] [ 1, 'B' ] [ 2, 'C' ]
console.log(...ids.entries());
console.log(...ids.keys()); // Logs out 0 1 2
console.log(...ids.values()); // Logs out A B C
let employee1 = { name: 'Jake' };
let employee2 = { name: 'Chris' };

let employees = new Map();
employees.set(employee1, 'ABC');
employees.set(employee2, '123');

console.log(employees.get(employee1));

console.log(employees.size); // Logs out 2

employees.delete(employee2);
console.log(employees.size); // Logs out 1

employees.clear();
console.log(employees.size); // Logs out 0


let arr = [
  [employee1, 'ABC'],
  [employee2, '123']
];

employees = new Map(arr);
console.log(employees.size); // Logs out 2

console.log(employees.has(employee2)); // Logs out True

let list = [...employees.values()];
console.log(list); // [ 'ABC', '123' ]

list = [...employees.entries()];
console.log(list); // [ [ { name: 'Jake' }, 'ABC' ], [ { name: 'Chris' }, '123' ] ]
console.log(list[0][1]); // ABC
let s = '8000';
console.log(isFinite(s));

let sum = 408.2;
console.log(Number.isInteger(sum));
let a =  {
  x: 1
};

let b =  {
  x: 3,
  y: 2
};

let c =  {
  z: 1
};

Object.setPrototypeOf(a, b);
Object.setPrototypeOf(b, c);
console.log(a.y); // Logs out 2

let target = {};
Object.assign(target, a, b); // Doesn't take properties set in setPrototypeOf
console.log(target); // logs out { x: 3, y: 2 }

let amount = NaN;
console.log(Object.is(amount, amount)); // fixes the === bug
let perks = new Set();

perks.add('Car');
perks.add('Vacation');
perks.add('Car');
perks.add('Jet');

// prints 2 because all of the items of the set have to be unique
console.log(perks.size);

console.log(perks.has('Jet')); // true
console.log(perks.has('Cool Hat')); // false

console.log(...perks.keys()); // Car Vacation Jet
console.log(...perks.values()); // Car Vacation Jet
console.log(...perks.entries()); // [ 'Car', 'Car' ] [ 'Vacation', 'Vacation' ] [ 'Jet', 'Jet' ]

perks = new Set([
  {id: 1},
  {id: 1}
]);

// 2 because object literals are unique
console.log(perks.size);
let title = 'Santa Barbara Surf Riders';

// Check if strings starts with substring
console.log(title.startsWith('Santa'));

// Check if strings ends with substring
console.log(title.endsWith('Santa'));

// Check if strings includes substring
console.log(title.includes('Surf'));

var title_with_emoji = "Surfer Emoji: \u{1f3c4}";
console.log(title_with_emoji);

// Prints out real length
console.log(title_with_emoji.normalize().length);

// Repeat String
console.log(title_with_emoji.repeat(3));
const CALCULATE_EVENT_SYMBOL = Symbol('resize event');
console.log(CALCULATE_EVENT_SYMBOL.toString());

以上是关于javascript ES6类型和对象扩展的主要内容,如果未能解决你的问题,请参考以下文章

三阶段课程——Day02(ES6简介let和const)(数据类型和数据结构:字符串扩展Symbol数组新增方法对象新增SetMap数组解构对象解构...运算符)

JavaScript ES6 - 对象扩展

JavaScript ES6 - 对象扩展

ES6随笔--各数据类型的扩展--数组和对象

Javascript ES6集合(SET)类型✪ ω ✪

JavaScript ES6 - 这是扩展语法还是休息语法?