zptime / blog

个人博客
0 stars 0 forks source link

前端基础知识之JS篇 #22

Open zptime opened 3 years ago

zptime commented 3 years ago

防抖和节流

判断是否为数组的方法

模块化发展历程

模块化主要是用来抽离公共代码,隔离作用域,避免变量冲突等。思维导图

全局作用域

变量生命周期:声明(作用域注册一个变量)、初始化(分配内存,初始化为 undefined)、赋值

箭头函数和普通函数的区别

箭头函数是普通函数的简写,可以更优雅的定义一个函数,和普通函数相比,有以下几点差异:

  1. 函数体内的 this 对象,就是定义时所在的对象(又说:定义时所在的作用域中的 this 值),而不是使用时所在的对象。
  2. 不可以使用 arguments 对象,该对象在函数体内不存在。如果要用,可以用 rest 参数代替。
  3. 不可以使用 yield 命令,因此箭头函数不能用作 Generator 函数。
  4. 不可以使用 new 命令,因为:
function newFunc(father, ...rest) {
  var result = {};
  result.__proto__ = father.prototype;
  var result2 = father.apply(result, rest);
  if (
    (typeof result2 === "object" || typeof result2 === "function") &&
    result2 !== null
  ) {
    return result2;
  }
  return result;
}

ES6 转 ES5 的思路

参考 Babel 的实现方式,其大致分为三步:

setTimeout、Promise、Async/Await 的区别

JS Intel 对象介绍

Intl 对象是 ECMAScript 国际化 API 的命名空间,它提供对语言敏感的字符串比较、支持数字格式化以及日期和时间的格式化。

// 控制台输入Intel,输出如下所示
{
  Collator: ƒ Collator()
  DateTimeFormat: ƒ DateTimeFormat()
  ListFormat: ƒ ListFormat()
  NumberFormat: ƒ NumberFormat()
  PluralRules: ƒ PluralRules()
  RelativeTimeFormat: ƒ RelativeTimeFormat()
  getCanonicalLocales: ƒ getCanonicalLocales()
  v8BreakIterator: ƒ v8BreakIterator()
}
// 数组字符串排序
["15", "2", "100"].sort(); // 结果是:["100", "15", "2"]
["15", "2", "100"].sort(
  new Intl.Collator(undefined, { numeric: true }).compare
); // 结果是:["2", "15", "100"]

// 中文按照首字母拼音排序
var arrUsername = [
  "陈坤",
  "邓超",
  "杜淳",
  "冯绍峰",
  "韩庚",
  "胡歌",
  "黄晓明",
  "贾乃亮",
  "李晨",
  "李易峰",
  "鹿晗",
  "井柏然",
  "刘烨",
  "陆毅",
  "孙红雷"
];
arrUsername.sort(); // 结果是:["井柏然", "冯绍峰", "刘烨", "孙红雷", "李易峰", "李晨", "杜淳", "胡歌", "贾乃亮", "邓超", "陆毅", "陈坤", "韩庚", "鹿晗", "黄晓明"]
arrUsername.sort(new Intl.Collator("zh").compare); // 结果是:["陈坤", "邓超", "杜淳", "冯绍峰", "韩庚", "胡歌", "黄晓明", "贾乃亮", "井柏然", "李晨", "李易峰", "刘烨", "陆毅", "鹿晗", "孙红雷"]
// 日期信息格式是:“xxxx年xx月xx日 xx:xx:xx”。Google和Firefox 2019/10/24 12:05:53, IE11 2019年10月24日 12:05:53
new Intl.DateTimeFormat("zh", {
  year: "numeric",
  month: "2-digit",
  day: "2-digit", // '2-digit'表示一定使用2位数字表示,因此,如果数值不超过10,会自动在前面加0。
  hour: "2-digit",
  minute: "2-digit",
  second: "2-digit",
  hour12: false // hour12设置为false表示我们采用24小时制,晚上是21:00而不是下午9点。
}).format(new Date());
// 1. 连续数字千位分隔符分隔
new Intl.NumberFormat(undefined, {
  minimumFractionDigits: 4
}).format(12345.6789); // 结果是:"12,345.6789"

// 2. 数字不足位数补0
new Intl.NumberFormat(undefined, {
  minimumIntegerDigits: 2
}).format(8); // 结果是:"08"

// 3. 金额中文自带
new Intl.NumberFormat("zh-Hans", {
  style: "currency",
  currency: "CNY",
  currencyDisplay: "name"
}).format(12345.6789); // 结果是:"12,345.68 人民币"

//4. 数字变成中文数字显示
"星期" + new Intl.NumberFormat("zh-Hans-CN-u-nu-hanidec").format(new Date().getDay()); // 结果是:"星期四"

JS DOM innerText 和 textContent 的区别

// 获取元素的文本内容,推荐使用textContent,兼容性写法如下:
var text = dom.textContent || dom.innerText;

JS DOM变化的监听检测

分析地址栏url,获取参数

浏览器已经有了内置的API接口可以对URL进行处理,这个API就是URLSearchParams()以及URL()

new URL('https://www.zhangxinxu.com/wordpress/?s=url').searchParams.get('s');
new URLSearchParams('?s=url').get('s');

form原生JS验证方法和属性

js工具

zepto和jquery的区别

js闭包this解决

var name = "The Window";
var object = {
    name: "My object",
    getNameFunc: function() {
        return function() {
            return this.name;
        };
    }
}
alert(object.getNameFunc()()); // "The Window"

把最后的一句拆成两个步骤执行:
var first = object.getNameFunc();
var second = first();
    其中第一步,获得的first为返回的匿名函数,此时的getNameFunc()作为object的方法调用,如果在getNameFunc()中使用this,此时的this指向的是object对象。
    第二部,调用first函数,可以很清楚的发现,此时调用first函数,first函数没有在对象中调用,因此是作为函数调用的,是在全局作用域下,因此first函数中的this指向的是window。
var name = "The Window";
var object = {
    name: "My object",
    getNameFunc: function() {
        var that = this;   // 将getNameFunc()的this保存在that变量中
        var age = 15;
        return function() {
            return that.name;
        };
    }
}
alert(object.getNameFunc()());   // "My object"
 其中,getNameFunc()执行时的活动对象有:that/age/匿名函数,在执行匿名函数时,同时引用了getNameFunc()中的活动对象,因此可以获取that和age的值。但是由于是在全局环境中调用的匿名函数,因此匿名函数内部的this还是指向window。