JavaScript学习

Array.prototype.indexOf()

**indexOf()**方法返回在数组中可以找到一个给定元素的第一个索引,如果不存在,则返回-1。

switch case

switch (sign) {
      case ORDER_SIGN[2]:
      case ORDER_SIGN[3]:
        return items;
      default:
        return '';
    }

像这种情况表示情况2和3都执行返回return,而不是情况2是没写或者是没有用

instanceof

instanceof 运算符用于检测构造函数的 prototype 属性是否出现在某个实例对象的原型链上。

function Car(make, model, year) {
  this.make = make;
  this.model = model;
  this.year = year;
}
const auto = new Car('Honda', 'Accord', 1998);

console.log(auto instanceof Car);
// expected output: true

console.log(auto instanceof Object);
// expected output: true

map的index从0开始计算

markers的巧妙获取labels

function initMap() {
  const map = new google.maps.Map(document.getElementById("map"), {
    zoom: 3,
    center: { lat: -28.024, lng: 140.887 },
  });
  // Create an array of alphabetical characters used to label the markers.
  const labels = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
  // Add some markers to the map.
  // Note: The code uses the JavaScript Array.prototype.map() method to
  // create an array of markers based on a given "locations" array.
  // The map() method here has nothing to do with the Google Maps API.
  const markers = locations.map((location, i) => {
    return new google.maps.Marker({
      position: location,
      label: labels[i % labels.length],
    });
  });

  // Add a marker clusterer to manage the markers.
  new MarkerClusterer(map, markers, {
    imagePath:
      "https://developers.google.com/maps/documentation/javascript/examples/markerclusterer/m",
  });
}

labels[i % labels.length]

labels.length比i的最大值大一

效果不是相当于

labels[i]

闭包

一个函数和对其周围状态(lexical environment,词法环境)的引用捆绑在一起(或者说函数被引用包围),这样的组合就是闭包closure)。也就是说,闭包让你可以在一个内层函数中访问到其外层函数的作用域。在 JavaScript 中,每当创建一个函数,闭包就会在函数创建的同时被创建出来。

react的行内样式

style={{ color:'red',zIndex:3}}

逻辑或判断

null || " " //" "
null || undefined //undefined
undefined || " " //" "
undefined || true //true
undefined || false //false


对象数组按要求查询某项的元素

const array1 = [5, 12, 8, 130, 44];

const isLargeNumber = (element) => element > 13;

console.log(array1.findIndex(isLargeNumber));
// expected output: 3

注意箭头函数的简写方式,以下是错误写法

const array1 = [5, 12, 8, 130, 44];

const isLargeNumber = (element) => {element > 13};

console.log(array1.findIndex(isLargeNumber));
// expected output: -1

不简写如下

const array1 = [5, 12, 8, 130, 44];

const isLargeNumber = (element) => {
if(element > 11){
return true}}

console.log(array1.findIndex(isLargeNumber));
// expected output: 3

如果是对象数组,最好使用Object.is去判断

console.log(options);   console.log(options.findIndex((item) => Object.is("EXPRESS", item.key)));

findIndex方法的使用,返回索引值

const arr2 = [{
  "corrected": [1, 2, 3, 4],
  "piece": 11,
  "pieceName": "数据1",
  "uncorrected": [10, 2, 3, 4]
}, {
  "corrected": [1, 2, 3, 4],
  "piece": 22,
  "pieceName": "数据2",
  "uncorrected": [55, 6, 7, 8]
}, {
  "corrected": [1, 2, 3, 4],
  "piece": 33,
  "pieceName": "数据3",
  "uncorrected": [99, 10, 11, 12]
}];
var piece = 33;
var rsIndex = arr2.findIndex((value) => Object.is(piece, value.piece))
console.log(rsIndex); // 2

debugger

function potentiallyBuggyCode() {
    debugger;
    // do potentially buggy stuff to examine, step through, etc.
}

Expected property shorthand报错

表示当前语法形式可以优化改写

ClustererArr.push({ lat: lat, lng: lng });

ClustererArr.push({ lat,lng });

Array.prototype.find方法

正确写法

arr.find(item => item.displayName = 'xxx')

如果写成这样

arr.find(item => {item.displayName = 'xxx'})

结果会变成undefined

Array.prototype.filter 也一样 ,can't use {},这会导致结果不准确

获取字符串最后一位

const index = event.target.id.substr(event.target.id.length - 1, 1);

'xxx'.substr('xxx'.length-1,1)

Array.prototype.findIndex()

**findIndex()**方法返回数组中满足提供的测试函数的第一个元素的索引。若没有找到对应元素则返回-1。

const array1 = [5, 12, 8, 130, 44];

const isLargeNumber = (element) => element > 13;

console.log(array1.findIndex(isLargeNumber));
// expected output: 3

indexOf 与 findIndex 区别

  • indexOf :查找值作为第一个参数,采用 === 比较,更多的是用于查找基本类型,如果是对象类型,则是判断是否是同一个对象的引用
  • findIndex :比较函数作为第一个参数,多用于非基本类型(例如对象)的数组索引查找,或查找条件很复杂
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • 1、JavaScript中如何检测一个变量是一个String类型?请写出函数实现 typeof(obj) === ...
    文朝明阅读 3,173评论 0 0
  • HTML&CSS 1.请描述一下 cookies,sessionStorage 和 localStorage 的区...
    qhaobaba阅读 2,642评论 0 1
  • HTML&CSS: 一.浏览器内核: 1.渲染引擎:渲染引擎负责取得网页的内容(HTML、XML、图像等等)、整理...
    GIDK阅读 3,110评论 0 0
  • 前端面试问题集锦 JavaScript 部分 1、JQuery $(document).ready() 和 win...
    涯无凌阅读 4,438评论 0 2
  • todo:总结下实际中js的一些注意事项、表格、全选、切换、模态框等 原则: 渐进增强 平稳退化 低耦合 JS脚本...
    flyrain阅读 3,207评论 0 0

友情链接更多精彩内容