• JS常用内置方法


    1.Math

    • Math.pow( a, b)a的b次方
    • Math.round( .6 ) 四舍五入
    • Math.ceil( .6 ) 向上求整
    • Math.floor( .6 )向下求整
    • Math.abs( -4 )求绝对值
    • Math.max( a,b,c ) 几个数中最大值
    • Math.min( a,b,c ) 几个数中最小值
    • Math.random( ) 生成一个大于0小于1的随机数
    • Math.pow( 3, 1/3 ) 3的立方根

    2.Array

    2.1 Array.from() 方法从一个类似数组或可迭代对象中创建一个新的,浅拷贝的数组实例。
    console.log(Array.from('foo'));
    // expected output: Array ["f", "o", "o"]
    console.log(Array.from([1, 2, 3], x => x + x));
    // expected output: Array [2, 4, 6]
    
    2.2 Array.isArray() 用于确定传递的值是否是一个 Array。
    Array.isArray([1, 2, 3]);  
    // true
    Array.isArray({foo: 123}); 
    // false
    Array.isArray("foobar");   
    // false
    Array.isArray(undefined);  
    // false
    
    2.3 Array.of() 方法创建一个具有可变数量参数的新数组实例,而不考虑参数的数量或类型。
    Array.of(7);       // [7] 
    Array.of(1, 2, 3); // [1, 2, 3]
    
    Array(7);          // [ , , , , , , ]
    Array(1, 2, 3);    // [1, 2, 3]
    
    2.4 concat() 方法用于合并两个或多个数组。此方法不会更改现有数组,而是返回一个新数组。
    const array1 = ['a', 'b', 'c'];
    const array2 = ['d', 'e', 'f'];
    const array3 = array1.concat(array2);
    
    console.log(array3);
    // expected output: Array ["a", "b", "c", "d", "e", "f"]
    
    

    ES6 方法

    const array1 = ['a', 'b', 'c'];
    const array2 = ['d', 'e', 'f'];
    console.log([...array1,...array2]);
    // expected output: Array ["a", "b", "c", "d", "e", "f"]
    
    2.5 filter() 方法创建一个新数组, 其包含通过所提供函数实现的测试的所有元素。
    const words = ['spray', 'limit', 'elite', 'exuberant', 'destruction', 'present'];
    
    const result = words.filter(word => word.length > 6);
    
    console.log(result);
    // expected output: Array ["exuberant", "destruction", "present"]
    
    
    2.6 find() 方法返回数组中满足提供的测试函数的第一个元素的值。否则返回 undefined。
    const array1 = [5, 12, 8, 130, 44];
    
    const found = array1.find(element => element > 10);
    
    console.log(found);
    // expected output: 12
    
    
    2.7 findIndex()方法返回数组中满足提供的测试函数的第一个元素的索引。否则返回-1。
    const array1 = [5, 12, 8, 130, 44];
    
    const isLargeNumber = (element) => element > 13;
    
    console.log(array1.findIndex(isLargeNumber));
    // expected output: 3
    
    2.8 forEach() 方法对数组的每个元素执行一次提供的函数。 遍历元素
    const array1 = ['a', 'b', 'c'];
    
    array1.forEach(element => console.log(element));
    
    // expected output: "a"
    // expected output: "b"
    // expected output: "c"
    
    2.9 includes() 方法用来判断一个数组是否包含一个指定的值,根据情况,如果包含则返回 true,否则返回false。
    const array1 = [1, 2, 3];
    
    console.log(array1.includes(2));
    // expected output: true
    
    const pets = ['cat', 'dog', 'bat'];
    
    console.log(pets.includes('cat'));
    // expected output: true
    
    console.log(pets.includes('at'));
    // expected output: false
    
    2.10 indexOf()方法返回在数组中可以找到一个给定元素的第一个索引,如果不存在,则返回-1。
    const beasts = ['ant', 'bison', 'camel', 'duck', 'bison'];
    
    console.log(beasts.indexOf('bison'));
    // expected output: 1
    
    // start from index 2
    console.log(beasts.indexOf('bison', 2));
    // expected output: 4
    
    console.log(beasts.indexOf('giraffe'));
    // expected output: -1
    
    
    2.11 join() 方法将一个数组(或一个类数组对象)的所有元素连接成一个字符串并返回这个字符串。如果数组只有一个项目,那么将返回该项目而不使用分隔符。
    const elements = ['Fire', 'Air', 'Water'];
    
    console.log(elements.join());
    // expected output: "Fire,Air,Water"
    
    console.log(elements.join(''));
    // expected output: "FireAirWater"
    
    console.log(elements.join('-'));
    // expected output: "Fire-Air-Water"
    
    
    2.12 map() 方法创建一个新数组,其结果是该数组中的每个元素都调用一个提供的函数后返回的结果。
    const array1 = [1, 4, 9, 16];
    
    // pass a function to map
    const map1 = array1.map(x => x * 2);
    
    console.log(map1);
    // expected output: Array [2, 8, 18, 32]
    
    
    2.13 pop()方法从数组中删除最后一个元素,并返回该元素的值。此方法更改数组的长度。
    const plants = ['broccoli', 'cauliflower', 'cabbage', 'kale', 'tomato'];
    
    console.log(plants.pop());
    // expected output: "tomato"
    
    console.log(plants);
    // expected output: Array ["broccoli", "cauliflower", "cabbage", "kale"]
    
    plants.pop();
    
    console.log(plants);
    // expected output: Array ["broccoli", "cauliflower", "cabbage"]
    
    2.14 push() 方法将一个或多个元素添加到数组的末尾,并返回该数组的新长度。
    const animals = ['pigs', 'goats', 'sheep'];
    
    const count = animals.push('cows');
    console.log(count);
    // expected output: 4
    console.log(animals);
    // expected output: Array ["pigs", "goats", "sheep", "cows"]
    
    animals.push('chickens', 'cats', 'dogs');
    console.log(animals);
    // expected output: Array ["pigs", "goats", "sheep", "cows", "chickens", "cats", "dogs"]
    
    
    2.15 reduce() 方法对数组中的每个元素执行一个由您提供的reducer函数(升序执行),将其结果汇总为单个返回值。
    const array1 = [1, 2, 3, 4];
    const reducer = (accumulator, currentValue) => accumulator + currentValue;
    
    // 1 + 2 + 3 + 4
    console.log(array1.reduce(reducer));
    // expected output: 10
    
    // 5 + 1 + 2 + 3 + 4
    console.log(array1.reduce(reducer, 5));
    // expected output: 15
    
    
    2.16 reverse() 方法将数组中元素的位置颠倒,并返回该数组。数组的第一个元素会变成最后一个,数组的最后一个元素变成第一个。该方法会改变原数组。
    const array1 = ['one', 'two', 'three'];
    console.log('array1:', array1);
    // expected output: "array1:" Array ["one", "two", "three"]
    
    const reversed = array1.reverse();
    console.log('reversed:', reversed);
    // expected output: "reversed:" Array ["three", "two", "one"]
    
    // Careful: reverse is destructive -- it changes the original array.
    console.log('array1:', array1);
    // expected output: "array1:" Array ["three", "two", "one"]
    
    
    2.17 shift() 方法从数组中删除第一个元素,并返回该元素的值。此方法更改数组的长度。
    const array1 = [1, 2, 3];
    
    const firstElement = array1.shift();
    
    console.log(array1);
    // expected output: Array [2, 3]
    
    console.log(firstElement);
    // expected output: 1
    
    
    2.18 unshift() 方法将一个或多个元素添加到数组的开头,并返回该数组的新长度(该方法修改原有数组)。
    const array1 = [1, 2, 3];
    
    console.log(array1.unshift(4, 5));
    // expected output: 5
    
    console.log(array1);
    // expected output: Array [4, 5, 1, 2, 3]
    
    
    2.19 slice() 方法返回一个新的数组对象,这一对象是一个由 begin 和 end 决定的原数组的浅拷贝(包括 begin,不包括end)。原始数组不会被改变。 截取数组
    const animals = ['ant', 'bison', 'camel', 'duck', 'elephant'];
    
    console.log(animals.slice(2));
    // expected output: Array ["camel", "duck", "elephant"]
    
    console.log(animals.slice(2, 4));
    // expected output: Array ["camel", "duck"]
    
    console.log(animals.slice(1, 5));
    // expected output: Array ["bison", "camel", "duck", "elephant"]
    
    
    2.20 splice() 方法通过删除或替换现有元素或者原地添加新的元素来修改数组,并以数组形式返回被修改的内容。此方法会改变原数组。
    const months = ['Jan', 'March', 'April', 'June'];
    months.splice(1, 0, 'Feb');
    // inserts at index 1
    console.log(months);
    // expected output: Array ["Jan", "Feb", "March", "April", "June"]
    
    months.splice(4, 1, 'May');
    // replaces 1 element at index 4
    console.log(months);
    // expected output: Array ["Jan", "Feb", "March", "April", "May"]
    
    

    3.Function

    3.1 apply() 方法调用一个具有给定this值的函数,以及作为一个数组(或类似数组对象)提供的参数。
    const numbers = [5, 6, 2, 3, 7];
    
    const max = Math.max.apply(null, numbers);
    
    console.log(max);
    // expected output: 7
    
    const min = Math.min.apply(null, numbers);
    
    console.log(min);
    // expected output: 2
    
    
    3.2 bind() 方法创建一个新的函数,在 bind() 被调用时,这个新函数的 this 被指定为 bind() 的第一个参数,而其余参数将作为新函数的参数,供调用时使用。
    const module = {
      x: 42,
      getX: function() {
        return this.x;
      }
    }
    
    const unboundGetX = module.getX;
    console.log(unboundGetX()); // The function gets invoked at the global scope
    // expected output: undefined
    
    const boundGetX = unboundGetX.bind(module);
    console.log(boundGetX());
    // expected output: 42
    
    
    3.3 call() 方法使用一个指定的 this 值和单独给出的一个或多个参数来调用一个函数。
    function Product(name, price) {
      this.name = name;
      this.price = price;
    }
    
    function Food(name, price) {
      Product.call(this, name, price);
      this.category = 'food';
    }
    
    console.log(new Food('cheese', 5).name);
    // expected output: "cheese"
    
    

    4.String

    4.1 concat() 方法将一个或多个字符串与原字符串连接合并,形成一个新的字符串并返回。
    var hello = "Hello, ";
    console.log(hello.concat("Kevin", " have a nice day.")); /* Hello, Kevin have a nice day. */
    
    4.2 includes() 方法用于判断一个字符串是否包含在另一个字符串中,根据情况返回 true 或 false。
    var str = 'To be, or not to be, that is the question.';
    
    console.log(str.includes('To be'));       // true
    console.log(str.includes('question'));    // true
    console.log(str.includes('nonexistent')); // false
    console.log(str.includes('To be', 1));    // false
    console.log(str.includes('TO BE'));       // false
    
    4.3 indexOf() 方法返回调用它的 String 对象中第一次出现的指定值的索引,从 fromIndex 处进行搜索。如果未找到该值,则返回 -1。
    "Blue Whale".indexOf("Blue");     // 返回  0
    "Blue Whale".indexOf("Blute");    // 返回 -1
    "Blue Whale".indexOf("Whale", 0); // 返回  5
    "Blue Whale".indexOf("Whale", 5); // 返回  5
    "Blue Whale".indexOf("", -1); // 返回 0
    "Blue Whale".indexOf("", 9);      // 返回  9
    "Blue Whale".indexOf("", 10);     // 返回 10
    "Blue Whale".indexOf("", 11);     // 返回 10
    
    
    4.4 padEnd() 方法会用一个字符串填充当前字符串(如果需要的话则重复填充),返回填充后达到指定长度的字符串。从当前字符串的末尾(右侧)开始填充。
    'abc'.padEnd(10);          // "abc       "
    'abc'.padEnd(10, "foo");   // "abcfoofoof"
    'abc'.padEnd(6, "123456"); // "abc123"
    'abc'.padEnd(1);           // "abc"
    
    4.5 replace() 方法返回一个由替换值(replacement)替换一些或所有匹配的模式(pattern)后的新字符串。模式可以是一个字符串或者一个正则表达式,替换值可以是一个字符串或者一个每次匹配都要调用的回调函数。
    const p = 'The quick brown fox jumps over the lazy dog. If the dog reacted, was it really lazy?';
    
    const regex = /dog/gi;
    
    console.log(p.replace(regex, 'ferret'));
    // expected output: "The quick brown fox jumps over the lazy ferret. If the ferret reacted, was it really lazy?"
    
    console.log(p.replace('dog', 'monkey'));
    // expected output: "The quick brown fox jumps over the lazy monkey. If the dog reacted, was it really lazy?"
    
    
    4.6 slice() 方法提取某个字符串的一部分,并返回一个新的字符串,且不会改动原字符串。
    const str = 'The quick brown fox jumps over the lazy dog.';
    
    console.log(str.slice(31));
    // expected output: "the lazy dog."
    
    console.log(str.slice(4, 19));
    // expected output: "quick brown fox"
    
    console.log(str.slice(-4));
    // expected output: "dog."
    
    console.log(str.slice(-9, -5));
    // expected output: "lazy"
    
    
    4.7 split() 方法使用指定的分隔符字符串将一个String对象分割成子字符串数组,以一个指定的分割字串来决定每个拆分的位置。
    const str = 'The quick brown fox jumps over the lazy dog.';
    
    const words = str.split(' ');
    console.log(words[3]);
    // expected output: "fox"
    
    const chars = str.split('');
    console.log(chars[8]);
    // expected output: "k"
    
    const strCopy = str.split();
    console.log(strCopy);
    // expected output: Array ["The quick brown fox jumps over the lazy dog."]
    
    
  • 相关阅读:
    初尝2D骨骼动画编辑工具SPINE,并into Unity3D
    Unity3D优化总结(一)
    ARPG游戏打击感相关的技术简单总结
    总结一下一般游戏中3D模型各种勾边方法遇到的工程性问题
    多次被坑了后,重温设计模式原则
    unity shader序列帧动画代码,顺便吐槽一下unity shader系统
    暗黑三泛光算法研究
    《全民无双》锁链刀光程序生成算法,即U3D——刀光轨迹插件的改进
    Low-level Thinking in High-level Shading Languages
    python实现智能语音天气预报
  • 原文地址:https://www.cnblogs.com/huangguofeng/p/13735866.html
Copyright © 2020-2023  润新知