Javascript
什么是位运算 现代计算机中所有的数据都以二进制的形式存储在设备中。即 0、1 两种状态,计算机对二进制数据进行的运算(+、-、*、/)都叫位运算,即将符号位共同参与运算的运算。
位运算 AND 当对一对数位执行位运算 AND 时,如果数位均为 1 则返回 1。
1、单位示例:
运算 结果 0 & 0 0 0 & 1 0 1 & 0 0 1 & 1 1 2、四位示例:
运算 结果 1111 & 0000 0000 1111 & 0001 0001 1111 & 0010 0010 1111 & 0100 0100 位运算 OR 当对 …
发布订阅模式,定义对象间的一种一对多的依赖关系,当一个对象的状态发生改变时,所有依赖于它的对象都将得到通知。在JS中,我们一般用事件模型来替代传统的发布-订阅模式。
1. 现实中的发布-订阅模式 购房者与售楼处;
购房者:订阅者; 售楼处:发布者;
2. 发布-订阅模式的作用 发布-订阅模式优点:
a. 发布-订阅模式可以广泛应用于异步编程中,这是一种替代传递回调函数的方案。在异步编程中,使用发布-订阅模式,我们就无需过多关注对象在异步运行期间的内部状态,而只需要订阅感兴趣的事件发生点。
b. 发布-订阅模式可以取代对象之间硬编码的通知机制,一个对象不用再显式地调用另外一个对象的某个接口。让两 …
代码 var getSingle = function (fn) { var result; return function () { return result || (result = fn.apply(this, arguments)); }; }; // 创建登录框 var createLoginDialog = function () { var div = document.createElement("div"); div.innerHTML = "Login Dialog"; div.style.display = "none"; …
1、Function declaration foo(); // "bar" function foo() { console.log('bar'); } 解释:
function foo() { console.log('bar'); } foo(); // "bar" 2、Function expression baz(); // TypeError: baz is not a function var baz = function() { console.log('bar2'); }; 解释:
var …
1、 console.log(x === undefined); // true var x = 3; 解释:
var x; console.log(x === undefined); // true x = 3; 2、 // will return a value of undefined var myvar = 'my value'; (function() { console.log(myvar); // undefined var myvar = 'local value'; })(); 解释:
var myvar = 'my …
将对象转换为Url拼接参数 const obj2str = obj => { let arr = Object.entries(obj); arr.forEach((value, index) => { arr[index] = value.join('='); }); return arr.join('&'); }; 实例:
var data = { "name": "jie", "age" : 20 }; console.log(obj2str(data)); //output: …
HTML结构 <section class="card"> <input type="file" class="input" accept="text/*" id="fileInput"/> <div class="preview" id="filePreview"></div> </section> 读取为文本内容 const fileInput = …
需求:将手机号码中间4位用 * 号替换,保护隐私。
使用正则 const encryptPhoneNumber = (str) => { if (str) { str = str.trim(); const reg = /(\d{3})\d{4}(\d{3})/; str = str.replace(reg, "$1****$2"); } return str; }; encryptPhoneNumber("18614023235"); //output: 186****3235 使用substring const encryptPhoneNumber = …