Skip to content
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

Open
jefferyE opened this issue Mar 15, 2019 · 82 comments
Open
Labels

Comments

@jefferyE
Copy link

jefferyE commented Mar 15, 2019

var b = 10;
(function b(){
   b = 20;
   console.log(b); 
})();

我的解法:
1)打印10

var b = 10;
(function b(b) {
 window.b = 20;
 console.log(b)
})(b)

或者

var b = 10;
(function b(b) {
 b.b = 20;
 console.log(b)
})(b)

2)打印20

var b = 10;
(function b(b) {
 b = 20;
 console.log(b)
})(b)

var b = 10;
(function b() {
 var b = 20;
 console.log(b)
})()
@XiaoLuo01
Copy link

打印 20 :

var b = 10;
(function (){
  b = 20;
  console.log(b);
})();

打印 10:

var b = 10;
(function (){
  console.log(b);
  b = 20;
})();

@jjeejj
Copy link
Contributor

jjeejj commented Mar 15, 2019

打印20
第一种方法:

var b = 10;
(function a(){
   b = 20;
   console.log(b); 
})();

第二种方法:

var b = 10;
(function b(){
   let b = 20;
   console.log(b); 
})();

对于该方法,按照昨天的解答 , b 不是无法修改的,但是现在修改成功了

第三种方法:

var b = 10;
(function (){
   b = 20;
   console.log(b); 
})();

打印10

var b = 10;
(function b(b){
   console.log(b); 
   b = 20;
})(b);

@IWANABETHATGUY
Copy link
Contributor

干脆一块改造了

var b = 10;
console.log(b);

(function b() {
  var b = 20;
  console.log(b);
})();

@ligoudan1
Copy link

打印20和10:

    var b = 10;
    (function b(b){
        b = 20;
        console.log(b); 
    })(b);
 console.log(b);

@qewqewzzz
Copy link

var b = 10;
(function b(b){
console.log(b);
b = 20;
console.log(b);
})(b);

@jackchenl
Copy link

jackchenl commented Mar 15, 2019

var b = 10;
(function b(){
    var b = 20; //or let b = 20;
    console.log(this.b);
    console.log(b); 
})();

@LaoLeo
Copy link

LaoLeo commented Mar 15, 2019

// 打印20
var b = 10;
(function b(b){
b = 20;
console.log(b);
})(b);

var b = 10;
(function (){
b = 20;
console.log(b);
})();

// 打印10
var b = 10;
(function b(){
b = 20;
console.log(this.b);
})();

@ghost
Copy link

ghost commented Mar 15, 2019

分别打印出 10 和 20

@bran-nie
Copy link

bran-nie commented Mar 15, 2019

var a = 10;
(function a() {
    var a = 20
    console.log(a)
})();
console.log(a);
// 20
// 10

在立即执行函数表达式里,对a进行var声明,是重新声明了一个变量

@dorseysen
Copy link

    var b = 10;
    (function b(b){
        console.log(window.b);
        b = 20;
        console.log(b); 
    })();

@y1324
Copy link

y1324 commented Mar 15, 2019

同时打印10 和20

var b = 10;
(function b() {
let b = 20;
console.log(b)
})(b);
console.log(b);

@chenchangyuan
Copy link

打印10

var b = 10;
(function b(){
   b = 20;
   console.log(window.b);//10 
})();

打印20

var b = 10;
(function b(){
   var b = 20;
   console.log(b);//20
})();

@DangoSky
Copy link

  var b = 10;
  (function b(){
    console.log(window.b);   // 打印10
    var b = 20;
    console.log(b);     // 打印20
  })();

@huanglvming
Copy link

var b = 10;
(function b(){
   b = 20;
   console.log(b); 
})();

我的解法:
1)打印10

var b = 10;
(function b(b) {
 window.b = 20;
 console.log(b)
})(b)

或者

var b = 10;
(function b(b) {
 b.b = 20;
 console.log(b)
})(b)

2)打印20

var b = 10;
(function b(b) {
 b = 20;
 console.log(b)
})(b)

var b = 10;
(function b() {
 var b = 20;
 console.log(b)
})()

麻烦问一下,打印10的第一种方法和打印20的第一种方法有什么不同,为什么输出的结果不一样?按照我的理解,打印20的第一种方法,函数里的b就是window.b,为什么输出结果不一样?

@DangoSky
Copy link

@huanglvming

var b = 10;
(function b(){
   b = 20;
   console.log(b); 
})();

我的解法:
1)打印10

var b = 10;
(function b(b) {
 window.b = 20;
 console.log(b)
})(b)

或者

var b = 10;
(function b(b) {
 b.b = 20;
 console.log(b)
})(b)

2)打印20

var b = 10;
(function b(b) {
 b = 20;
 console.log(b)
})(b)

var b = 10;
(function b() {
 var b = 20;
 console.log(b)
})()

麻烦问一下,打印10的第一种方法和打印20的第一种方法有什么不同,为什么输出的结果不一样?按照我的理解,打印20的第一种方法,函数里的b就是window.b,为什么输出结果不一样?

  打印10的第一种方法:b已经作为参数被传进函数里去了,即使函数内使用 window.b = 20;改变了全局的b也已经对它没有影响了,所以会打印出原先的10。
  打印20的第一种方法:函数里的b并不是全局的b,修改它也不会影响到全局的b。
  你可能没有注意到一点,基本数据类型的传递只是传递数值,比如此处不管你修改的是函数外的b还是函数内的b都不会对对方造成影响。对象的传递才是按引用传递,一个变了全部都要变,可以了解一下js里关于基本数据类型和引用数据类型内存空间的存储方式。

@y1324
Copy link

y1324 commented Mar 19, 2019

var b =  10 ; 
( function  b (){ 
   b =  20 ;
    console . log (b); 
})();

我的解法:
1)打印10

var b =  10 ; 
( function  b ( b ) {
  window . b  =  20 ;
  console . log (b) 
})(b)

或者

var b =  10 ; 
( function  b ( b ) {
  b . b  =  20 ;
  console . log (b) 
})(b)

2)打印20

var b =  10 ; 
( function  b ( b ) { 
 b =  20 ;
  console . log (b) 
})(b)

var b =  10 ; 
( function  b () {
  var b =  20 ;
  console . log (b) 
})()

麻烦问一下,打印10的第一种方法和打印20的第一种方法有什么不同,为什么输出的结果不一样?按照我的理解,打印20的第一种方法,函数里的b就是window.b,为什么输出结果不一样?

两个作用域不同,所以打印的不一样,第一个是外层的b,window上的。20的第一个打印的函数里面的,b只是在作用于函数里面。

@mengfei-nie
Copy link

不是很理解 这个自执行函数里面的 变量被重新赋值后 为啥还是输出之前的变量值函数

@shizhenbin
Copy link

console.log(10);
console.log(20);

@jjeejj
Copy link
Contributor

jjeejj commented Mar 26, 2019

console.log(10);
console.log(20);

你是来搞笑的吗?????? 😅

@hmmoshang
Copy link

打印10
var b = 10;
(function b(){
b = 20;
console.log(window.b);
})();
打印20
(function (){
b = 20;
console.log(b);
})();

@zhw2590582
Copy link

这题目问得不是很严谨

@frjcxy
Copy link

frjcxy commented Mar 30, 2019

console.log(10);
console.log(20);

你是来搞笑的吗?????? 😅兄台厉害

@pangqiang
Copy link

var b = 10;
(function b(b){
    console.log(b)
    b = 20;
    console.log(b);
})(b);

@yu910709
Copy link

yu910709 commented Apr 3, 2019

打印10

var b = 10;
(function b(){
   b = 20;
   console.log(window.b);//10 
})();

打印20

var b = 10;
(function b(){
   var b = 20;
   console.log(b);//20
})();

找到一个答案一样的,又没说不能改打印体😂

@yu910709
Copy link

yu910709 commented Apr 3, 2019

不是很理解 这个自执行函数里面的 变量被重新赋值后 为啥还是输出之前的变量值函数

看第33题

@qinnnn
Copy link

qinnnn commented Apr 9, 2019

var b = 10;
(function b(){
    var b = 20;
    console.log(this.b);
	console.log(b);
})();

@kevinlorry
Copy link

我写一下我的理解和解法吧(既然改写应该还是要在原来题目上最小改动)
var b = 10;
(function b(){
var b = 20;
console.log(b);
})();

var b = 10;
(function b(){
b = 20;
console.log(window.b);
})();

@chenshiai
Copy link

console.log(10,20)
应题目要求,不考虑出题者的意图,第一反应是这个(手动滑稽)

@Abiel1024
Copy link

var b = 10;
(function b(){
    b = 20;
    console.log(this.b); 
})();
// 10
var b = 10;
(function b(){
    let b = 20;
    console.log(b); 
})();
// 20

@witsaint
Copy link

既然能够简单修改代码
打印20
var b = 10; (function(){ b = 20; console.log(b); })();
打印10
var b = 10; (function(){ console.log(b); })();

@yipjianbo
Copy link

console.log(10);
console.log(20);

特地登陆进来点个赞,这老哥机灵鬼,笑死我了

@yipjianbo
Copy link

var b = 10;
(function b(){
   b = 20;
   console.log(b); 
})();

输出10

var b = 10;
(function b(){
   b = 20;
   console.log(this.b); 
})();

输出20

var b = 10;
(function b(){
   var b = 20;
   console.log(b); 
})();

@zhangqiong27
Copy link

原题:
var b = 10;
(function b(){
b = 20;
console.log(b);
})();
打印20:
(function b(b){
b = 20;
console.log( b);
})(b); // 20

(function b(){
    let b = 20;
    console.log(b);
})();  // 20

打印10:
(function b(){
b = 20;
console.log( this.b);
})(); // 10

@Hideer
Copy link

Hideer commented Feb 29, 2020

image

@QZEming
Copy link

QZEming commented Mar 6, 2020

原题

var b = 10;
(function b(){
    b = 20;
    console.log(b); 
})();

皮一下
打印10

var b = 10;
(function b(){
    b = 20;
    console.log(10); 
})();

打印20

var b = 10;
(function b(){
    b = 20;
    console.log(20); 
})();

认真做题
打印10

var b = 10;
(function b(){
    b = 20;
    console.log(this.b); 
})();

打印20

var b = 10;
(function b(){
    var b = 20;
    console.log(b); 
})();

@devinxiang
Copy link

var b = 10;
(function b(b){
    console.log(b)
    var b = 20;
    console.log(b);
})(b);

@cMYour
Copy link

cMYour commented Apr 7, 2020

// 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); 
})();

@zhangwen1932
Copy link

  var b = 10;
 (function b() {
      let b=20;
      console.log(window.b);
      console.log(b);
 })();

@codexu
Copy link

codexu commented May 27, 2020

console.log(10)
console.log(20)

@jackchang2015
Copy link

jackchang2015 commented Jun 6, 2020

//打印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); 
})();

@soraly
Copy link

soraly commented Jun 17, 2020

var b = 10;
console.log(b);  //10
(function b(){
    var b = 20;
    console.log(b);  //20
})();

@zengkaiz
Copy link

// 打印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);

@RoeyXie
Copy link

RoeyXie commented Aug 3, 2020

// 20

var b = 10;
(function b(){
    var b = 20;
    console.log(b); 
})();

// 10

var b = 10;
(function b(){
    b = 20;
    console.log(window.b); 
})();

@LoveFreedom97
Copy link

Emmmm
var b = 10;
(function(){
console.log(b)
b=20;
console.log(b)
})()

@zelixag
Copy link

zelixag commented Aug 18, 2020

这个题目问的不太好,函数表达式的name属于函数作用域的且不能重新复制,类似const定义,可以用来调用。

@lsczy
Copy link

lsczy commented Aug 25, 2020

var b = 10; 
(function b(b) {
   b = 20;
   console.log(window.b) // 10
   console.log(b)        // 20
})(b)

@HeartYangj
Copy link

var b = 10;
(function b(b) {
console.log(b);
b = 20;
console.log(b);
})(b);

@m7yue
Copy link

m7yue commented Sep 11, 2020

// 10
var b = 10;
(function b(){
    b = 20;
    console.log(window.b); 
})();

// 20
var b = 10;
(function b(){
    let b = 20;
    console.log(b); 
})();

@BlackCloverM
Copy link

console.log(10);
console.log(20);

你是来搞笑的吗?????? 😅

真把我逗笑了

@BarneyRoos
Copy link

//不太明白这题在考啥
var b = 10;
(function b(num){
    console.log(num)
    num = 20;
    console.log(num);
})(b);

@wangsiyuan4
Copy link

var b = 10;
(function b() {
console.log(window.b)
let b = 20;
console.log(b);
})();

@Yangfan2016
Copy link

// 打印 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)

@weijuer
Copy link

weijuer commented Nov 17, 2022

// 10
var b = 10;
(function (){
    console.log(b); 
    b = 20;
})();

// 20
var b = 10;
(function (){
    b = 20;
    console.log(b); 
})();

@18920199681
Copy link

var b = 10;
(function b(b){
    b = 20;
    console.log(this.b); 
    console.log(b); 
})(b);

@liyi711
Copy link

liyi711 commented Dec 22, 2022

打印20:

var b = 10;
(function b(){
  var  b = 20;
   console.log(b);
})()

打印10:

var b = 10;
(function b(){
  b = 20;
   console.log(this.b);
})()

@XW666
Copy link

XW666 commented Apr 28, 2023

 var b = 10;
  (function b() {
    var b = 20;
    console.log(b);
    console.log(this.b)
  })();

1 similar comment
@XW666
Copy link

XW666 commented Apr 28, 2023

 var b = 10;
  (function b() {
    var b = 20;
    console.log(b);
    console.log(this.b)
  })();

@Chenmin926
Copy link

// 打印 10
var b = 10;
(function b(){
b = 20;
console.log(window.b);
})();

// 打印20
var b = 10;
(function b(){
window.b = 20;
console.log(window.b);
})();

@npmrun
Copy link

npmrun commented Aug 2, 2023

console.log(10);
console.log(20)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests