347 lines
5.3 KiB
Markdown
347 lines
5.3 KiB
Markdown
# this练习
|
||
注:document.write的题目 需要写出在页面上打印的结果
|
||
1. 下面代码的执行结果是什么 为什么
|
||
```
|
||
var person = {
|
||
firstName: "John",
|
||
lastName : "Doe",
|
||
id : 5566,
|
||
fullName : function() {
|
||
return this.firstName + " " + this.lastName;
|
||
}
|
||
};
|
||
|
||
// 显示对象的数据
|
||
document.getElementById("demo").innerHTML = person.fullName();
|
||
|
||
```
|
||
|
||
2. 下面代码的执行结果是什么 为什么
|
||
|
||
```
|
||
var x = this;
|
||
document.getElementById("demo").innerHTML = x;
|
||
|
||
```
|
||
|
||
3. 下面代码的执行结果是什么 为什么
|
||
|
||
```
|
||
"use strict";
|
||
var x = this;
|
||
document.getElementById("demo").innerHTML = x;
|
||
|
||
```
|
||
|
||
4. 下面代码的执行结果是什么 为什么
|
||
|
||
```
|
||
document.getElementById("demo").innerHTML = myFunction();
|
||
function myFunction() {
|
||
return this;
|
||
}
|
||
|
||
```
|
||
|
||
5. 下面代码的执行结果是什么 为什么
|
||
|
||
```
|
||
"use strict";
|
||
document.getElementById("demo").innerHTML = myFunction();
|
||
function myFunction() {
|
||
return this;
|
||
}
|
||
|
||
```
|
||
|
||
6. 下面代码的执行结果是什么 为什么
|
||
|
||
```
|
||
<body>
|
||
|
||
<h2>JavaScript <b>this</b> 关键字</h2>
|
||
|
||
<button onclick="this.style.display='none'">点我后我就消失了</button>
|
||
|
||
</body>
|
||
|
||
```
|
||
|
||
|
||
7. 下面代码执行结果是什么 为什么
|
||
```
|
||
var person = {
|
||
firstName : "John",
|
||
lastName : "Doe",
|
||
id : 5566,
|
||
myFunction : function() {
|
||
return this;
|
||
}
|
||
};
|
||
|
||
// 显示表单数据
|
||
document.getElementById("demo").innerHTML = person.myFunction();
|
||
|
||
```
|
||
8. 把第7题的对象中的属性和方法挨个打印在页面上
|
||
9. 下面代码的执行结果是什么 为什么
|
||
|
||
```
|
||
var person = {
|
||
firstName: "John",
|
||
lastName : "Doe",
|
||
id : 5566,
|
||
fullName : function() {
|
||
return this.firstName + " " + this.lastName;
|
||
}
|
||
};
|
||
|
||
// 显示对象的数据
|
||
document.getElementById("demo").innerHTML = person.fullName();
|
||
|
||
```
|
||
|
||
10. 下面代码的执行结果是什么 为什么
|
||
|
||
```
|
||
var person1 = {
|
||
fullName: function() {
|
||
return this.firstName + " " + this.lastName;
|
||
}
|
||
}
|
||
var person2 = {
|
||
firstName:"John",
|
||
lastName: "Doe",
|
||
}
|
||
person1.fullName.call(person2);
|
||
|
||
```
|
||
|
||
11. 下面代码的执行结果是什么 为什么
|
||
|
||
```
|
||
function speak(){
|
||
var name = this.name
|
||
console.log(`Hello I am ${name}`)
|
||
}
|
||
var me = {
|
||
name: 'a',
|
||
speak: speak
|
||
}
|
||
var you = {
|
||
name: 'b',
|
||
speak: speak
|
||
}
|
||
me.speak()
|
||
you.speak()
|
||
|
||
```
|
||
|
||
12. 下面代码的执行结果是什么 为什么
|
||
|
||
```
|
||
function fn(){
|
||
console.log(this.name)
|
||
}
|
||
fn.name = 'xxx'
|
||
fn()
|
||
|
||
```
|
||
|
||
13. 下面代码的执行结果是什么 为什么
|
||
|
||
```
|
||
function foo() {
|
||
var a = 2;
|
||
this.bar();
|
||
}
|
||
function bar() {
|
||
console.log( this.a );
|
||
}
|
||
foo();
|
||
|
||
```
|
||
|
||
14. 下面代码的执行结果是什么 为什么
|
||
|
||
```
|
||
function foo(){
|
||
console.log(this.a)
|
||
}
|
||
var a = 2
|
||
foo()
|
||
|
||
```
|
||
|
||
15. 下面代码的执行结果是什么 为什么
|
||
|
||
```
|
||
function foo() {
|
||
console.log( this.a );
|
||
}
|
||
var obj = {
|
||
a: 2,
|
||
foo: foo
|
||
};
|
||
obj.foo();
|
||
|
||
```
|
||
|
||
14. 下面的fn中的this指向谁
|
||
```
|
||
obj1.obj2.obj3.fn()
|
||
```
|
||
|
||
15. 下面代码的执行结果是什么 为什么
|
||
|
||
```
|
||
function foo() {
|
||
console.log( this.a );
|
||
}
|
||
var obj = {
|
||
a: 2,
|
||
foo: foo
|
||
};
|
||
var bar = obj.foo; // 函数别名!
|
||
var a = "xxxxx"
|
||
bar();
|
||
|
||
```
|
||
|
||
16. 下面代码的执行结果是什么 为什么
|
||
|
||
```
|
||
function foo() {
|
||
console.log( this.a );
|
||
}
|
||
var obj = {
|
||
a: 2,
|
||
foo: foo
|
||
};
|
||
var a = "xxxxx"
|
||
setTimeout( obj.foo ,100);
|
||
|
||
```
|
||
|
||
17. 下面代码的执行结果是什么 为什么
|
||
|
||
```
|
||
function foo(something) {
|
||
console.log( this.a, something );
|
||
return this.a + something;
|
||
}
|
||
function bind(fn, obj) {
|
||
return function() {
|
||
return fn.apply( obj, arguments );
|
||
};
|
||
}
|
||
var obj = {
|
||
a:2
|
||
};
|
||
var bar = bind( foo, obj );
|
||
var b = bar( 3 ); // 2 3
|
||
console.log( b );
|
||
|
||
|
||
```
|
||
|
||
18. 下面代码的执行结果是什么 为什么
|
||
|
||
```
|
||
const test = {
|
||
prop: 42,
|
||
func: function() {
|
||
return this.prop;
|
||
},
|
||
};
|
||
|
||
console.log(test.func());
|
||
|
||
```
|
||
|
||
19. 下面代码的执行结果是什么 为什么
|
||
|
||
```
|
||
console.log(this === window);
|
||
|
||
a = 37;
|
||
console.log(window.a);
|
||
|
||
this.b = "MND";
|
||
console.log(window.b)
|
||
console.log(b)
|
||
|
||
```
|
||
|
||
20. 下面代码的执行结果是什么 为什么
|
||
|
||
```
|
||
function f1(){
|
||
return this;
|
||
}
|
||
//在浏览器中:
|
||
f1() === window; //在浏览器中,全局对象是window
|
||
|
||
//在Node中:
|
||
f1() === globalThis;
|
||
|
||
|
||
```
|
||
|
||
21. 下面代码的执行结果是什么 为什么
|
||
```
|
||
function f2(){
|
||
"use strict"; // 这里是严格模式
|
||
return this;
|
||
}
|
||
|
||
f2() === undefined;
|
||
|
||
```
|
||
|
||
22. 下面代码的执行结果是什么 为什么
|
||
|
||
```
|
||
var obj = {a: 'Custom'};
|
||
var a = 'Global';
|
||
|
||
function whatsThis() {
|
||
return this.a;
|
||
}
|
||
|
||
whatsThis();
|
||
whatsThis.call(obj);
|
||
whatsThis.apply(obj);
|
||
|
||
```
|
||
|
||
23. 下面代码的执行结果是什么 为什么
|
||
|
||
```
|
||
function C(){
|
||
this.a = 37;
|
||
}
|
||
|
||
var o = new C();
|
||
console.log(o.a);
|
||
|
||
|
||
function C2(){
|
||
this.a = 37;
|
||
return {a:38};
|
||
}
|
||
|
||
o = new C2();
|
||
console.log(o.a);
|
||
|
||
```
|
||
|
||
24. 下面代码的执行结果是什么 为什么
|
||
|
||
```
|
||
<button onclick="alert(this.tagName.toLowerCase());">
|
||
Show this
|
||
</button>
|
||
|
||
|
||
``` |