this指向?

this is set according to how the method is called, and not according to how the method is written. So for obj.method(), this will be set to obj inside of method(). For obj.method.call(x), this inside of method() will be set to x. It is determined by how it’s called. What that also means is that if you pass it as a callback to e.g. onclick, this will be set to the global window object rather than what you expect.

参考1:https://stackoverflow.com/questions/13224620/the-this-keyword-returns-the-window-object-within-an-objects-prototype-in-jav

参考2:https://segmentfault.com/a/1190000008400124

JavaScript 函数中的 this 指向并不是在函数定义的时候确定的,而是在调用的时候确定的。换句话说,函数的调用方式决定了 this 指向

继续阅读this指向?

微信分享地址传参时的问题

问题描述:当微信分享地址携带两个相同的参数时,请求地址中会携带两个参数,但是复制出来的地址却只携带了其中一个参数(导致定位问题失败)

附带替换参数值代码:

changeUrlKeyVal: function(key, val) {
  const href = location.href.split(“#”)[0];
  const pattern = key + ‘=([^&]*)’;
  const replaceText = key + ‘=’ + val;
  return href.match(pattern) ? href.replace(eval(‘/(‘ + key + ‘=)([^&]*)/gi’), replaceText) : (href.match(‘[\?]’) ?href + ‘&’ + replaceText : href + ‘?’ + replaceText);
}

注意!!!谨记!!!

 

闭包是函数和声明该函数的词法环境的组合

转载自MDN 闭包:https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Closures

1.词法作用域中使用的域,是变量在代码中声明的位置所决定的。嵌套的函数可以访问在其外部声明的变量。

function init() {
    var name = "Mozilla"; // name is a local variable created by init
    function displayName() { // displayName() is the inner function, a closure
        alert (name); // displayName() uses variable declared in the parent function    
    }
    displayName();    
}
init();

继续阅读闭包是函数和声明该函数的词法环境的组合