location对象
location对象的常用属性和方法包括:
location.href:获取或设置当前URL。
location.protocol:获取当前协议,比如http、https等。
location.host:获取当前主机名和端口号。
location.pathname:获取当前路径。
location.search:获取当前查询参数。
location.hash:获取当前页面锚点。
e.preventDefault():阻止默认事件
<script type="text/javascript">
var inin = document.querySelector('input');
inin.addEventListener('click', function (e) {
e.preventDefault()
})
</script>
e.stopPropagation():阻止冒泡事件
<script type="text/javascript">
var lili = document.querySelector('li');
lili.addEventListener('click', function (e) {
e.stopPropagation()
})
</script>
arr.forEach(item=>{})用例
用例:统计字符串中每个字符的出现频率,返回一个 Object,key 为统计字符,value 为出现频率
function count(str) {
var arr = str.split('');
var obj = {};
arr.forEach(item=>{
if (obj[item]){
obj[item] += 1
}else{
obj[item] = 1
}
})
return obj
}
this指向
当函数作为对象的方法被调用时,this指向该对象。代码示例如下:
this指向obj,下面代码可以返回a和b的和
var obj = {
a: 1,
b: 2,
fn: function(){
return this.a + this.b
}
}
伪数组
- 有数组的属性:length
- 按索引方式储存数据
- 不具有数组的push(),forEach()等方法
- 本质上是一个object
- 常见的伪数组:函数内部的
arguments、
DOM 对象列表(比如通过document.getElementsByTags
得到的列表)、jQuery
对象(比如$("div")
)
Math .pow(number,power) 幂次计算
Math .pow(2, 3) : 2的3次方
Math.sqrt(number)平方根
Math .sqrt(4) :结果是2
Object.keys(object)
以数组形式返回object对象的所有键
function _keys(object) {
return Object.keys(object)
}
console.log(_keys({name: 'nowcoder', age: 7})); // [ "name", "age" ]
去除字符串中的空格(replace()方法)
replace 不改变原始字符串
乱七八糟的这一串是正则表达式啊....看不懂,还要学....
去除字符串内所有的空格:str = str.replace(/\s*/g,“”)
去除字符串内两头的空格:str = str.replace(/^\s*|\s*$/g,“”)
去除字符串内左侧的空格:str = str.replace(/^\s*/,“”)
去除字符串内右侧的空格:str = str.replace(/(\s*$)/g,“”)
去除字符串中的空格(trim()方法)
trim()不改变原数组
去除字符串内两头的空格:str.trim();
去除字符串内左侧的空格:str.trimStart();
去除字符串内右侧的空格:str.trimEnd()
时间戳转换年月日时分秒
之前看过,忘记了...,举例子:
function _date(number) {
var date = new Date(number);
return date.getFullYear() + '-' + (date.getMonth()+1) + '-' + date.getDate()
}
- 时间戳 = timer
- timer.getFullYear()
- getMonth()
- getDate()
- getHours()
- getMinutes()
- getSeconds()
const now = new Date();
const nowTime = now.getTime(); //获取当前日期的时间戳
时间戳转换为天 时 分 秒
function format(time){
const day = Math.floor(time/(1000*60*60*24))
const hours = Math.floor((time/(1000*60*60))%24)
const minutes = Math.floor((time/(1000*60))%60)
const seconds = Math.floor((time/1000)%60)
return day+'天'+hours+'小时'+minutes+'分钟'+seconds+'秒'
}
今天学的乱七八糟
还有就是根本记不住啊啊啊啊 看过就忘了啊啊!!