前言
我是歌谣 最好的种树是十年前 其次是现在 今天继续给大家带来的是this指向的讲解
环境配置
代码语言:javascript复制npm init -y
yarn add vite -D
修改page.json配置端口
代码语言:javascript复制{
"name": "demo1",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"dev": "vite --port 3002"
},
"keywords": [],
"author": "",
"license": "ISC",
"devDependencies": {
"vite": "^4.4.9"
}
}
案例1
代码语言:javascript复制function Test(b){
this.d=3;
var a=1;
function c(){
}
}
Test(123)
console.log(window.d)
//
运行结
案例2
代码语言:javascript复制function Test(){
this.name='123'
}
var test=new Test()
console.log(test.name)
function Person(name,age){
this.name="geyao"
this.age=18
}
function Programmer(name,age){
Person.apply(this,[name,age])
this.work="Programmer"
}
var p=new Programmer("geyao",18)
console.log(p)
运行结果