-
Notifications
You must be signed in to change notification settings - Fork 3.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
第 34 题:简单改造下面的代码,使之分别打印 10 和 20。 #51
Comments
打印 20 : var b = 10;
(function (){
b = 20;
console.log(b);
})(); 打印 10: var b = 10;
(function (){
console.log(b);
b = 20;
})(); |
打印20 var b = 10;
(function a(){
b = 20;
console.log(b);
})(); 第二种方法: var b = 10;
(function b(){
let b = 20;
console.log(b);
})();
第三种方法: var b = 10;
(function (){
b = 20;
console.log(b);
})(); 打印10 var b = 10;
(function b(b){
console.log(b);
b = 20;
})(b); |
干脆一块改造了 var b = 10;
console.log(b);
(function b() {
var b = 20;
console.log(b);
})(); |
打印20和10:
|
var b = 10; |
var b = 10;
(function b(){
var b = 20; //or let b = 20;
console.log(this.b);
console.log(b);
})(); |
// 打印20 var b = 10; // 打印10 |
var a = 10;
(function a() {
var a = 20
console.log(a)
})();
console.log(a);
// 20
// 10 在立即执行函数表达式里,对a进行var声明,是重新声明了一个变量 |
|
同时打印10 和20 var b = 10; |
打印10
打印20
|
|
麻烦问一下,打印10的第一种方法和打印20的第一种方法有什么不同,为什么输出的结果不一样?按照我的理解,打印20的第一种方法,函数里的b就是window.b,为什么输出结果不一样? |
打印10的第一种方法:b已经作为参数被传进函数里去了,即使函数内使用 |
两个作用域不同,所以打印的不一样,第一个是外层的b,window上的。20的第一个打印的函数里面的,b只是在作用于函数里面。 |
不是很理解 这个自执行函数里面的 变量被重新赋值后 为啥还是输出之前的变量值函数 |
console.log(10); |
你是来搞笑的吗?????? 😅 |
打印10 |
这题目问得不是很严谨 |
|
|
找到一个答案一样的,又没说不能改打印体😂 |
看第33题 |
var b = 10;
(function b(){
var b = 20;
console.log(this.b);
console.log(b);
})(); |
我写一下我的理解和解法吧(既然改写应该还是要在原来题目上最小改动) var b = 10; |
console.log(10,20) |
|
既然能够简单修改代码 |
|
|
原题:
打印10: |
原题 var b = 10;
(function b(){
b = 20;
console.log(b);
})(); 皮一下 var b = 10;
(function b(){
b = 20;
console.log(10);
})(); 打印20 var b = 10;
(function b(){
b = 20;
console.log(20);
})(); 认真做题 var b = 10;
(function b(){
b = 20;
console.log(this.b);
})(); 打印20 var b = 10;
(function b(){
var b = 20;
console.log(b);
})(); |
|
// 10
var b = 10;
(function b(){
b = 20;
// 此时的 this 指向 window 即:全局变量
console.log(this.b);
})();
// 20
var b = 10;
(function b(){
this.b = 20;
console.log(this.b);
})(); |
var b = 10;
(function b() {
let b=20;
console.log(window.b);
console.log(b);
})(); |
console.log(10) |
//打印10
var b = 10;
(function (){
console.log(b);
})();
//
var b = 10;
(function b(){
b = 20;
console.log(this.b);
})();
//打印20
var b = 10;
(function (){
b = 20;
console.log(b);
})();
//
var b = 10;
(function b(){
let b = 20;
console.log(b);
})(); |
|
// 打印20
var b = 10;
(()=>{
b = 20
console.log(b)
})()
// 打印20
var b = 10;
(function b(b){
b = 20;
console.log(b);
})();
// 打印20
var b = 10;
(function b(){
var b = 20;
console.log(b);
})();
// 打印10
var b = 10;
(function b(b){
window.b = 20;
console.log(b);
})(b); |
// 20
// 10
|
Emmmm |
这个题目问的不太好,函数表达式的name属于函数作用域的且不能重新复制,类似const定义,可以用来调用。 |
|
var b = 10; |
// 10
var b = 10;
(function b(){
b = 20;
console.log(window.b);
})();
// 20
var b = 10;
(function b(){
let b = 20;
console.log(b);
})(); |
真把我逗笑了 |
//不太明白这题在考啥
var b = 10;
(function b(num){
console.log(num)
num = 20;
console.log(num);
})(b); |
var b = 10; |
// 打印 10
var b = 10;
(function b(c) {
b = 20;
console.log(c)
})(b)
// 打印20
var b = 10;
(function b(b) {
b = 20;
console.log(b)
})(b)
|
// 10
var b = 10;
(function (){
console.log(b);
b = 20;
})();
// 20
var b = 10;
(function (){
b = 20;
console.log(b);
})(); |
var b = 10;
(function b(b){
b = 20;
console.log(this.b);
console.log(b);
})(b); |
打印20:
打印10:
|
|
1 similar comment
|
// 打印 10 // 打印20 |
console.log(10); |
我的解法:
1)打印10
或者
2)打印20
或
The text was updated successfully, but these errors were encountered: