如何判断字符串中某个特定字符的个数
String.prototype.getInCludes = function (x){
return (this.split(x)).length-1
}
创建一个函数,给定页面上的DOM元素,将访问元素本身及其后代(不仅仅是它的子元素)。对于每个访问的元素,函数应该将该元素传递给提供的回调函数。
function Traverse(p_element,p_callback){
p_callback(p_element);
var list = p_element.children;
for(let i = 0; i < list.length; i++){
Traverse(list[i],p_callback);
}
}
let a = a 会是什么结果 为什么?
Uncaught ReferenceError: a is not defined
分为2个阶段:1、在预编译阶段,将let声明的变量放到暂存性死区TDZ中 TDZ=[a]
2、当let声明语句结束之后,这里的结束指的是,当let a = a执行完事后,会把a从TDZ拿出来。
但是let a = a执行的时候,右侧赋值的a还在TDZ中,所以报错。也就是未声明就使用的错误。
所以,大家在使用let变量时,切记一定要先声明后使用
typeof bar==='object’来确定bar是否是一个对象有什么缺陷 如何避免?
typeof null 也等于object !
所以可以使用((bar !== null) && (typeof bar ==="object"))
来判断 //true
但是如果bar是一个函数 上面的解决方案就为false,如果你希望为true 可以用如下:
((bar !== null) && (typeof bar ==="object") && (typeof bar ==="function"))
但是如果bar是一个数组 上面的解决方案就为true, 例如var bar = [] 如果你希望为false 可以用如下:
((bar !== null) && (typeof bar ==="object") && (Object.prototype.toString(bar)! =="[object Array]"))
还有一个方法对空置、数组、函数返回false 但对于对象为true:
((bar !== null) && (bar.constructor === Object))
或者使用JQuery:
((bar !== null) && (typeof bar ==="object") && (!&.isArray(bar))
请简单实现双向数据绑定mvvm
<input id="input"/>
<span id="show"></span>
const data = {};
const input = document.getElementById('input');
const span= document.getElementById('span');
Object.defineProperty(data, 'text', {
set(value) {
input.value = value;
this._value = value;
},
get(){
span.innerText= this._value
}
});
input.onchange = function(e) {
data.text = e.target.value;
}