var Car = function() {
console.log(this); // window
console.log(this.Car==window.Car,Car==window.Car); // true true
}
Car();
<script>
1. this代表所在function被哪一个对象调用了,那么这个this就代表这个对象。
2. 如果没有明确的调用对象,则代表window
function fn(){
alert(this);
}
fn(); window //没有明确对象,所以代表
document.onclick = fn; document
var obj = {fn: fn}
obj.fn(); obj
'use strict';
a = 3;
alert(a);
function fn(){
'use strict';
alert(this); undefined
}
fn();
</script>
2. this出现在函数作用域严格模式中,永远不会指向window
函数中使用ES5的严格模式‘use strict',this为undefined
function car() {
'use strict'
console.log(this); // undefined
}
car();
3. 当某个函数为对象的一个属性时,在这个函数内部this指向这个对象
var car = {
name:'丰田',
run() {
console.log(this); // {name: "丰田", run: ƒ}
}
}
4. this出现在构造函数中,指向构造函数新创建的对象
var Car = function(name) {
this.name = name;
console.log(this); // Car {name: "亚洲龙"}
// Car {name: "汉兰达"}
}
var myCar_1 = new Car('亚洲龙');
var myCar_2 = new Car('汉兰达');
5. 当一个元素被绑定事件处理函数时,this指向被点击的这个元素
var btn = document.querySelector('button');
btn.onclick = function() {
console.log(this); // <button>this</button>
}
6. this出现在箭头函数中时,this和父级作用域的this指向相同
const obj = {
Car() {
setTimeout(function() {
setTimeout(function() {
console.log(this); // window
})
setTimeout(()=>{
console.log(this); // window
})
})
setTimeout(() => {
setTimeout(function() {
console.log(this); // window
})
setTimeout(()=>{
console.log(this); // obj
})
})
}
}
obj.Car();
<script>
var div = document.querySelector('#box');
var inp = document.querySelector('#txt');
document.onclick = function(){
alert(this); //document
setTimeout(function(){
alert(this); //window,想让this指向document
}.bind(this), 3000); //document
}
function fn(){
alert(this);
}
fn.call(div); // div
fn.apply(document); //document
fn.bind(inp)(); // input
</script>
let
和 const
的区别function fn() {} // 不能简写
const fun = function () {} // 可以简写
const obj = {
fn: function () {} // 可以简写
}
语法: (函数的行参) => { 函数体内要执行的代码 }
const fn = function (a, b) {
console.log(a)
console.log(b)
}
// 可以使用箭头函数写成
const fun = (a, b) => {
console.log(a)
console.log(b)
}
function fn(a) {
a = a || 10
console.log(a)
}
fn() // 不传递参数的时候,函数内部的 a 就是 10
fn(20) // 传递了参数 20 的时候,函数内部的 a 就是 20
function fn(a = 10) {
console.log(a)
}
fn() // 不传递参数的时候,函数内部的 a 就是 10
fn(20) // 传递了参数 20 的时候,函数内部的 a 就是 20
const fn = (a = 10) => {
console.log(a)
}
fn() // 不传递参数的时候,函数内部的 a 就是 10
fn(20) // 传递了参数 20 的时候,函数内部的 a 就是 20
let str = `hello world`
console.log(typeof str) // stringX
和单引号还有双引号的区别
// 这个单引号或者双引号不能换行,换行就会报错了
let str = 'hello world'
// 下面这个就报错了
let str2 = 'hello
world'
let str = `
hello
world
`
console.log(str) // 是可以使用的
反引号可以直接在字符串里面拼接变量
// ES5 需要字符串拼接变量的时候
let num = 100
let str = 'hello' + num + 'world' + num
console.log(str) // hello100world100
// 直接写在字符串里面不好使
let str2 = 'hellonumworldnum'
console.log(str2) // hellonumworldnum
// 模版字符串拼接变量
let num = 100
let str = `hello${num}world${num}`
console.log(str) // hello100world100
let {a = 1,b = 2,c = 3} = {c : 8,a : 2,b : 3}
let [a = 1,b = 2,c = 3] = [4,5,6];
let arr = [1, 2, 3, 4, 5]
console.log(...arr) // 1 2 3 4 5
合并数组的时候可以使用
let arr = [1, 2, 3, 4]
let arr2 = [...arr, 5]
console.log(arr2)
也可以合并对象使用
let obj = {
name: 'Jack',
age: 18
}
let obj2 = {
...obj,
gender: '男'
}
console.log(obj2)
在函数传递参数的时候也可以使用
let arr = [1, 2, 3]
function fn(a, b, c) {
console.log(a)
console.log(b)
console.log(c)
}
fn(...arr)
// 等价于 fn(1, 2, 3)
当对象中的key和value的名字相同时,可以只写一个key.
let id = 1;
let name = '手机';
let price = 4999;
//创建一个对象
let obj = {
id, // id : id 名字相同,可以简写
name,
price,
num : 2
}
//定义模块
let user = '张三';
let age = 18;
function show(){
return '姓名' + user + '年龄' + age;
}
//导出模块
export {user,age,show};
//导入模块
import {user,age,show} from './tools.js';
2. 边定义模块,边导出模块
//边定义模块,边导出模块
export let user = '张三';
export let age = 18;
export function show(){
return `姓名${user},年龄${age}`;
}
//导入模块
import {user,age,show} from './tools.js';
3.以别名的方式导出模块
let a = '李四';
let b = 19;
function c(){
return `姓名:${a},年龄:${b}`;
}
export {a as user,b as age,c as show};
//导入模块
import {user,age,show} from './tools.js';
4.导入 导出默认模块
//导出默认模块(只能有一个)
let user = '王五';
let age = 20;
function a(){
return `姓名:${user},年龄:${age}`;
}
export {user,age};
export default a;
//导入模块
import {user,age} from './tools.js';
//导入默认模块
import display from './tools.js';
let set = new Set(); let set = new Set([1,2,1,2,1,2,3]);
2. 属性
size : 长度
3. 方法:
set.add(元素) : 添加元素,返回set对象 set.has(元素) : 检测元素是否在set对象中,返回布尔值 set.delete(元素) : 删除指定元素,返回布尔值 set.clear() : 清空set对象 set.forEach((value,key,set) =>{}) : 遍历set对象 set.keys() : 获取所有的key set.values() : 获取所有的value set.entries() : 获取所有的key和value for of
for(循环变量 of set|map){
语句组;
}
let map = new Map(); let map = new Map([ [1,'one'], [2,'two'], ['2','three'], [true,'four'], [3,'five'] ]);
2. 属性
size : 长度
3. 方法:
map.set('key','value') : 添加元素,返回map对象 map.get('key') : 获取value map.has(元素) : 检测元素是否在map对象中,返回布尔值 map.delete(元素) : 删除指定元素,返回布尔值 map.clear() : 清空map对象 map.forEach((value,key,map) =>{}) : 遍历map对象 map.keys() : 获取所有的key map.values() : 获取所有的value map.entries() : 获取所有的key和value for of
for(循环变量 of set|map){
语句组;
}
阅读量:523
点赞量:0
收藏量:0