赞
踩
使用闭包的注意点
栈和堆的区别:
- function creatPerson(name, age) {
- var obj = new Object();
- obj.name = name;
- obj.age = age;
- obj.sayName = function () {
- alert(this.name);
- }
- return obj;
- }
- function Person(name, age) {
- this.name = name;
- this.age = age;
- this.sayName = function () {
- alert(this.name);
- }
- }
- function Person () {};
- Person.prototype = {
- constructor: Person,
- name: "ZhangSan",
- age: "20",
- sayName: function () {
- alert(this.name);
- }
- }
- function Person(name, age) {
- this.name = name;
- this.age = age;
- }
- Person.prototype = {
- constructor: Person,
- sayName: function () {
- alert(this.name);
- }
- }
- function SuperType(name) {
- this.name = name;
- this.sayName = function () {
- alert(this.name);
- }
- }
-
- function SubType(name, age) {
- SuperType.call(this, name); // 这里借用了父类的构造函数
- this.age = age;
- }
- function SuperType(name) {
- this.name = name;
- this.sayName = function() {
- alert(this.name);
- }
- }
-
- function SubType(name, age) {
- this.supertype = SuperType; // 在这里使用了对象冒充
- this.supertype(name);
- this.age = age;
- }
- function SuperType(name) {
- this.name = name;
- }
-
- SuperType.prototype = {
- sayName: function () {
- alert(this.name);
- }
- }
-
- function SubType(name, age) {
- SuperType.call(this, name); // 在这里继承了属性
- this.age = age;
- }
-
- SubType.prototype = new SuperType(); // 这里继承方法

- // 1、创建连接
- var xhr = null;
- xhr = new XMLHttpRequest();
- // 2、连接服务器
- xhr.open('get', url, true);
- // 3、发送请求
- xhr.send(null);
- // 4、接受请求
- xhr.onreadystatechange = function () {
- if (xhr.readyState == 4) {
- if (xhr.status == 200) {
- success(xhr.responseText);
- } else {
- // false
- fail && fail(xhr.status);
- }
- }
- }

- // 异步加载地图
- export default function MapLoader() {
- return new Promise((resolve, reject) => {
- if (window.AMap) {
- resolve(window.AMap);
- } else {
- var script = document.createElement('script');
- script.type = 'text/javascript';
- script.async = ture;
- script.src = '';
- script.onerror = reject;
- document.head.appendChild(script);
- }
- window.initAMap = () => {
- resolve(window.AMap);
- }
- })
- }

var obj = eval('(' + str + ')');
- parseInt('1', 0); // radix为0时,使用默认的10进制。
- parseInt('2', 1); // radix值在2-36,无法解析,返回NaN
- parseInt('3', 2); // 基数为2,2进制数表示的数中,最大值小于3,无法解析,返回NaN
变量必须声明后再使用
函数的参数不能有同名属性,否则报错
不能使用with语句
禁止this指向全局对象
- var obj = eval('(' + str + ')');
- var obj = str.parseJSON();
- var obj = JSON.parse(str);
- var last = obj.toJSONString();
- var last = JSON.stringify(obj);
- // instanceof运算符是用来测试一个对象是否在其原型链原型构造函数的属性
- var arr = [];
- arr instanceof Array; // true
- // constructor属性返回对创建此对象的数组函数的引用,就是返回对象相对应的构造函数
- var arr = [];
- arr.constructor == Array; // true
- var a = new Array(123);
- var b = new Date();
- console.log(Array.isArray(a)); // true
- console.log(Array.isArray(b)); // false
- function unique(arr) {
- for (var i = 0; i < arr.length; i++) {
- for (var j = i + 1; j < arr.length; j++) {
- if (arr[i] === arr[j]) { // 第一个等同于第二个,删除第二个
- arr.splice(j, 1);
- j--;
- }
- }
- }
- return arr;
- }
- var arr = [1, 1, 'true', 'true', true, true, 15, false, undefined, undefined, null];
- console.log(unique(arr));
- function unique(arr) {
- return Array.from(new Set(arr));
- }
- var arr = [1, 1, 'true', 'true', true, true, 15, false, undefined, undefined, null];
- console.log(unique(arr));
- function unique(arr) {
- if (!Array.isArray(arr)) {
- console.log('type error');
- return;
- }
- var array = [];
- for (var i = 0; i < arr.length; i++) {
- if (array.indexOf(arr[i]) === -1) {
- array.push(arr[i]);
- }
- }
- return array;
- }
- var arr = [1, 1, 'true', 'true', true, true, 15, false, undefined, undefined, null];
- console.log(unique(arr));
- function unique(arr) {
- if (!Array.isArray(arr)) {
- console.log('type error');
- return;
- }
- arr = arr.sort();
- var array = [arr[0]];
- for (var i = 1; i < arr.length; i++) {
- if (arr[i] !== arr[i - 1]) {
- array.push(arr[i]);
- }
- }
- return array;
- }
- var arr = [1, 1, 'true', 'true', true, true, 15, false, undefined, undefined, null];
- console.log(unique(arr));

- function unique(arr) {
- if (!Array.isArray(arr)) {
- console.log('type error');
- return;
- }
- var array = [];
- var obj = {};
- for (var i = 0; i < arr.length; i++) {
- if (!obj[arr[i]]) {
- array.push(arr[i]);
- obj[arr[i]] = 1;
- } else {
- obj[arr[i]]++;
- }
- }
- return array;
- }
- var arr = [1, 1, 'true', 'true', true, true, 15, false, undefined, undefined, null];
- console.log(unique(arr));

强缓存
协商缓存
- // *使用 underscore 的源码来解释防抖动
- // *underscore 防抖函数,返回函数连续调用时,空闲时间必须大于或等于 wait,func才会执行
- // *@param { function } func 回调函数
- // *@param { number } wait 表示时间窗口的间隔
- // *@param { boolean } immediate 设置为true时,是否立即调用函数
- // *@param { function } 返回客户调用函数
-
- _.debounce = function (func, wait, immediate) {
- var timeout, args, context, timestamp, result;
- var later = function () {
- // 现在和上一次时间戳比较
- var last = _.now() - timestamp;
- // 如果当前时间间隔少于设定时间且大于0就重新设置定时器
- if (last < wait && last >= 0) {
- timeout = setTimeout(later, wait - last);
- } else {
- // 否则的话就是时间到了,执行回调函数
- timeout = null;
- if (!immediate) {
- result = func.apply(context, args);
- if (!timeout) context = args = null;
- }
- }
- };
-
- return function () {
- context = this;
- args = arguments;
- // 获得时间戳
- timestamp = _.now();
- // 如果定时器不存在且立即执行函数
- var callNow = immediate && !timeout;
- // 如果定时器不存在就创建一个
- if (!timeout) timeout = setTimeout(later, wait);
- if (callNow) {
- // 如果需要立即执行函数的话,通过 apply 执行
- result = func.apply(context, args);
- context = args = null;
- }
- return result;
- }
- }

- /**
- * underscore 节流函数,返回函数连续调用时,func 执行频率限定为 次 / wait
- * @param { function } func 回调函数
- * @param { number } wait 表示时间窗口的间隔
- * @param { object } options 如果想忽略开始函数的的调用,传入{ leading: false }。
- * 如果想忽略结尾函数的调用,传入{ trailing: false }
- * 两者不能共存,否则函数不能执行
- * @return { function } 返回客户调用函数
- **/
-
- _.throttle = function (func, wait, options) {
- var context, args, result; var timeout = null; var previous = 0;
- // 如果 options 没传则设为空对象 if (!options) options = {};
- // 定时器回调函数
- var later = function () {
- // 如果设置了 leading,就将 previous 设为 0
- // 用于下面函数的第一个 if 判断
- previous = options.leading === false ? 0 : _.now();
- result = func.apply(context, args);
- if (!timeout) context = args = null;
- };
- return function () {
- var now = _.now();
- // 首次进入前者肯定为 true
- // 如果需要第一次不执行函数
- // 就将上次时间戳设为当前的
- // 这样在接下来计算 remaining 的值时会大于 0 if (!previous && options.leading === false) previous = now;
- // 计算剩余时间
- var remaining = wait - (now - previous);
- context = this; args = arguments;
- // 如果当前调用已经大于上次调用时间 + wait
- // 如果设置了 trailing,只会进入这个条件
- // 如果没有设置 leading,那么第一次会进入这个条件
- // 还有一点,你可能会觉得开启了定时器那么应该不会进入这个 if 条件了
- // 其实还是会进入的,因为定时器的延时
- // 并不是准确的时间,很可能你设置了 2 秒
- // 但是他需要 2.2 秒才触发,这时候就会进入这个条件
- if (remaining <= 0 || remaining > wait) {
- // 如果存在定时器就清理掉否则会调用二次回调
- if (timeout) {
- clearTimeout(timeout);
- timeout = null;
- } previous = now; result = func.apply(context, args);
- if (!timeout) context = args = null;
- } else if (!timeout && options.trailing !== false) {
- // 判断是否设置了定时器和 trailing
- // 没有的话就开启一个定时器
- // 并且不能,不能同时设置 leading 和 trailing
- timeout = setTimeout(later, remaining);
- } return result;
- };
- };

- var instance = null;
- class Storage {
- static getInstance() {
- if (!instance) {
- instance = new Storage();
- }
- return instance;
- }
- setItem = (key, value) => localStorage.setItem(key, value);
- getItem = key => localStorage.getItem(key);
- }
- var arr = [3,1,5,6,8,2,9,7,4];
- function bubbleSort(arr) {
- for (var i = 0; i < arr.length; i++) {
- for (var j = 0; j < arr.length - i - 1; j++) {
- if (arr[j + 1] < arr[j]) {
- var temp;
- temp = arr[j];
- arr[j] = arr[j + 1];
- arr[j + 1] = temp;
- }
- }
- }
- return arr;
- }
- console.log(bubbleSort(arr));
- var arr = [1, 1, 'true', 'true', true, true, 15, false, undefined, undefined, null];
- Array.prototype.unique = function () {
- return [...new Set(this)];
- }
- console.log(arr.unique(arr));
- // 方法一
- function isArray(arg) {
- if (typeof arg === 'object') {
- return Object.prototype.toSting.call(arg) === '[object Array]';
- }
- return false;
- }
-
- // 方法二
- function isArray2(arg) {
- return Array.isArray(arg);
- }
有错请指正,侵删
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。