100字范文,内容丰富有趣,生活中的好帮手!
100字范文 > 变量提升 作用域this实战(真题:看代码输出)

变量提升 作用域this实战(真题:看代码输出)

时间:2022-11-19 08:41:10

相关推荐

变量提升 作用域this实战(真题:看代码输出)

1. 原型的应用

//function A() {this.name = 'smyhvae';}A.prototype.test = function () {setTimeout(function () {console.log('this.name');}, 1)}var a = new A();a.test(); // this.name//this.a = 20function Go () {console.log(this.a) // 40this.a = 30}Go.prototype.a = 40console.log(new Go().a) // 30//window.name = 'ByteDance';function A() {this.name = 123;}A.prototype.getA = function() {return this.name + 1; };let a = new A();let funcA = a.getA;console.log(funcA()); // ByteDance1console.log(a.getA()); //124//Function.prototype.a = ()=>console.log(1)Object.prototype.b = ()=>console.log(2);function A(){}const a = new A();a.b(); // 2// a.a(); //报错

2. 函数局部作用域在函数结束后销毁

//var name = '全局'function getName() {var name = '局部';return this.name;};console.log(getName()) // 全局//let val = 1;function foo() {console.log(val);}function bar() {let val = 2;foo();}bar(); // 1//let a={b:110}function aaa(o){o.b=120o={b:119}}console.log(aaa(a)) // undefinedconsole.log(a) // {b: 120}

3. 注意对象的引用

//var obj ={a:4}function test(obj) {obj.a =3}test(obj)console.log(obj.a) // 3 //var length = 10function fn() {return this.length + 1};var obj = {length: 5,test1() {return fn()}}console.log(obj.test1()) // 11obj.test2 = fnconsole.log(obj.test1() === obj.test2()) // false

let obj = {a: 0};function test(obj) {obj.a = 1;obj = {a: 2};obj.b = 3;console.log(obj) // {a: 2, b: 3}}test(obj);console.log(obj); // {a: 1}

let obj = {a: 0,};function test(obj) {obj = {a: 2,};obj.a = 1;obj.b = 3;console.log(obj); //{a: 1, b: 3}}test(obj);console.log(obj); //{a: 0}

4. 箭头函数.call无效

// const school = {name: "大哥",}function getName() {console.log("getName:" + this.name); }getName1 = () => console.log("getName1:" + this.name); window.name = "张三";getName(); // getName:张三getName1(); // getName1:张三getName.call(school); // getName:大哥getName1.call(school); // getName1:张三//const obj = {birth: 1990,getAge(year) {let fn = y => y - this.birth;return fn.call({birth: 2000}, year);},};console.log(obj.getAge()); //30

var a = 1;var obj = {a: 2};function fun() {var a = 3;let f = () => console.log(this.a);f();};fun();//1fun.call(obj);//2

5.函数作为对象键值

var id = 'GLOBAL';var obj = {id: 'OBJ',a: function() {console.log(this.id);},b: () => {console.log(this.id);}};obj.a(); // 'OBJ'obj.b(); // 'GLOBAL'new obj.a() // undefinednew obj.b() // Uncaught TypeError: obj.b is not a constructor

6. 非strictmodel的情况下:函数内部用var声明的变量为显示全局变量,最高提升到当前(函数)作用域;不用var声明的变量为隐式全局变量,会提升到window。(strictmodel的情况:都不会提升到window)

window本身就是一个内置的对象,访问一个内置的对象无论什么属性,有值就显示值没有值就显示undefined

let obj = {a: 0};function test(obj) {obj = {a: 3};var obj1 = {a: 3};obj2 = {a: 3};let obj3 = {a: 3};}test(obj);console.log(obj); // a: 0console.log(window.obj); // undefined// console.log(obj1); console.log(window.obj1); //undefinedconsole.log(obj2); //a: 3console.log(window.obj2); //a: 3// console.log(obj3);console.log(window.obj3); //undefined

const name = 'list'function test() {console.log(this.name) // ''}let Obj = {name: 'aaa',people: () => {console.log(this.name) // ''test()},}Obj.people()

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。