随笔分类 -  ES5

解决构造函数的缺陷
摘要:index.js function Animal(race) { this.race = race; } Animal.prototype.eat = function () { console.log(`${this.race} is eatting.`); }; const bird = new 阅读全文

posted @ 2021-09-10 10:40 aisowe 阅读(60) 评论(0) 推荐(0) 编辑

将数组中的空元素转为 undefined
摘要:index.js const list = [1, , 3]; Array.apply(null, list); // [1, undefined, 3] [...list]; // [1, undefined, 3] 阅读全文

posted @ 2021-09-10 10:11 aisowe 阅读(153) 评论(0) 推荐(0) 编辑

将类数组对象转换成数组
摘要:index.js const arrayLikeObj = { 0: 1, length: 2 }; [].slice.apply(arrayLikeObj); // [1, empty] Array.prototype.slice.apply(arrayLikeObj); // [1, empty 阅读全文

posted @ 2021-09-10 10:09 aisowe 阅读(56) 评论(0) 推荐(0) 编辑

将代码推迟到系统资源空闲时执行
摘要:index.js requestIdleCallback(myNonEssentialWork); function myNonEssentialWork(deadline) { while (deadline.timeRemaining() > 0) { doWorkIfNeeded(); } } 阅读全文

posted @ 2021-09-10 10:08 aisowe 阅读(32) 评论(0) 推荐(0) 编辑

检查某个对象是否为另一个对象的原型
摘要:index.js function Foo() {} const foo = new Foo(); console.log(foo instanceof Foo); // true // 等价于 console.log(Foo.prototype.isPrototypeOf(foo)); // tr 阅读全文

posted @ 2021-09-10 10:05 aisowe 阅读(28) 评论(0) 推荐(0) 编辑

类型转换总结
摘要:Boolean null, undefined, false, NaN, '', +0, -0, 0n --> false,其它均为 true Number 能转则转,不能则 NaN(undefined),特例 Symbol 会报错。 String "true", "0", "1,2", "[obj 阅读全文

posted @ 2021-09-10 10:01 aisowe 阅读(12) 评论(0) 推荐(0) 编辑

获取实例对象的原型对象
摘要:index.js const instance = {}; console.log(instance.__proto__ Object.prototype); // true, 浏览器才需要部署 console.log(instance.constructor.prototype Object.pr 阅读全文

posted @ 2021-09-10 09:54 aisowe 阅读(99) 评论(0) 推荐(0) 编辑

获取浏览器当前位置
摘要:index.js window.screenX window.screenY 阅读全文

posted @ 2021-09-10 09:53 aisowe 阅读(129) 评论(0) 推荐(0) 编辑

获取某个对象的原型对象
摘要:index.js function Person() {} Person.prototype.sayHello = function () {}; console.log(Object.getPrototypeOf(new Person())); // {sayHello: ƒ, construct 阅读全文

posted @ 2021-09-10 09:53 aisowe 阅读(55) 评论(0) 推荐(0) 编辑

获取构造函数的名称
摘要:index.js // 构造函数和一般函数没有区别,都可以通过 name 属性获取函数名 function Person() {} const lilei = new Person(); console.log(lilei.constructor.name); // 'Person' console 阅读全文

posted @ 2021-09-10 09:52 aisowe 阅读(70) 评论(0) 推荐(0) 编辑

获取对象的所有属性,不管是否可遍历,不管是自身的还是原型链上的
摘要:index.js function Person() { this.race = "human being"; // 实例属性 } Person.prototype.color = "yellow"; // 公共属性/原型属性 const instance = new Person(); insta 阅读全文

posted @ 2021-09-10 09:51 aisowe 阅读(72) 评论(0) 推荐(0) 编辑

获取参数对象本身的所有属性的键名数组
摘要:index.js const instance = { name: "lilei", age: 13, }; Object.defineProperty(instance, "gender", { value: 1, enumerable: false, }); Object.getOwnPrope 阅读全文

posted @ 2021-09-10 09:47 aisowe 阅读(56) 评论(0) 推荐(0) 编辑

获取 Object.prototype 的原型对象
摘要:index.js const objProto = {}.__proto__ || Object.getPrototypeOf({}) || Object.prototype; console.log(Object.getPrototypeOf(objProto) || objProto.__pro 阅读全文

posted @ 2021-09-10 09:46 aisowe 阅读(91) 评论(0) 推荐(0) 编辑

怎样理解数组的空元素empty与undefined的区别
摘要:有三种方法: 1. Function.prototype.call(); 2. Function.prototype.apply(); 3. Function.prototype.bind(); 一、Function.prototype.call() 可以指定函数在参数对象环境下执行; var ob 阅读全文

posted @ 2021-09-10 09:43 aisowe 阅读(132) 评论(0) 推荐(0) 编辑

怎样绑定/固定/切换this的指向
摘要:有三种方法: 1. Function.prototype.call(); 2. Function.prototype.apply(); 3. Function.prototype.bind(); 一、Function.prototype.call() 可以指定函数在参数对象环境下执行; var ob 阅读全文

posted @ 2021-09-10 09:43 aisowe 阅读(54) 评论(0) 推荐(0) 编辑

固定内层函数的 this
摘要:index.js const obj = { foo() { console.log(this); // obj const self = this; const bar = (function () { console.log(self); // obj })(); }, }; obj.foo() 阅读全文

posted @ 2021-09-10 09:40 aisowe 阅读(18) 评论(0) 推荐(0) 编辑

防止漏写 new 而造成的构造函数执行异常
摘要:index.js // 方法1:严格模式 function Person(name) { "use strict"; this.name = name; } const lilei = Person("李雷"); // Error console.log(lilei.name); index.js 阅读全文

posted @ 2021-09-10 09:36 aisowe 阅读(41) 评论(0) 推荐(0) 编辑

创建一个原型对象为 null 的对象
摘要:index.js const foo = Object(null); const bar = Object.create(null); console.log(foo instanceof Object); // true console.log(bar instanceof Object); // 阅读全文

posted @ 2021-09-09 10:37 aisowe 阅读(116) 评论(0) 推荐(0) 编辑

创建一个不含原型对象的对象
摘要:index.js const specialObj = Object.create(null); console.log(specialObj.__proto__); // undefined 阅读全文

posted @ 2021-09-09 10:35 aisowe 阅读(278) 评论(0) 推荐(0) 编辑

ES5 创建构造函数的私有属性
摘要:index.js function Foo() { const list = []; this.push = function (val) { list.push(val); }; this.getList = function () { console.log(list); }; } const 阅读全文

posted @ 2021-09-09 10:34 aisowe 阅读(120) 评论(0) 推荐(0) 编辑

导航

< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

统计

点击右上角即可分享
微信分享提示