Git Product home page Git Product logo

Comments (82)

XiaoLuo01 avatar XiaoLuo01 commented on May 3, 2024 100

打印 20 :

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

打印 10:

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

from daily-interview-question.

IWANABETHATGUY avatar IWANABETHATGUY commented on May 3, 2024 75

干脆一块改造了

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

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

from daily-interview-question.

shizhenbin avatar shizhenbin commented on May 3, 2024 60

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

from daily-interview-question.

jackchenl avatar jackchenl commented on May 3, 2024 19
var b = 10;
(function b(){
    var b = 20; //or let b = 20;
    console.log(this.b);
    console.log(b); 
})();

from daily-interview-question.

 avatar commented on May 3, 2024 15

分别打印出 10 和 20

from daily-interview-question.

jjeejj avatar jjeejj commented on May 3, 2024 12

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

from daily-interview-question.

ligoudan1 avatar ligoudan1 commented on May 3, 2024 7

打印20和10:

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

from daily-interview-question.

qewqewzzz avatar qewqewzzz commented on May 3, 2024 7

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

from daily-interview-question.

chenchangyuan avatar chenchangyuan commented on May 3, 2024 6

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

from daily-interview-question.

HeartYangj avatar HeartYangj commented on May 3, 2024 6

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

from daily-interview-question.

chenshiai avatar chenshiai commented on May 3, 2024 5

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

from daily-interview-question.

dorseysen avatar dorseysen commented on May 3, 2024 3
    var b = 10;
    (function b(b){
        console.log(window.b);
        b = 20;
        console.log(b); 
    })();

from daily-interview-question.

DangoSky avatar DangoSky commented on May 3, 2024 2

@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里关于基本数据类型和引用数据类型内存空间的存储方式。

from daily-interview-question.

zhw2590582 avatar zhw2590582 commented on May 3, 2024 2

这题目问得不是很严谨

from daily-interview-question.

icedJuice avatar icedJuice commented on May 3, 2024 2

同时打出 10 跟 20

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

只打出10

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

只打20

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

from daily-interview-question.

jackchang2015 avatar jackchang2015 commented on May 3, 2024 2
//打印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); 
})();

from daily-interview-question.

bran-nie avatar bran-nie commented on May 3, 2024 1
var a = 10;
(function a() {
    var a = 20
    console.log(a)
})();
console.log(a);
// 20
// 10

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

from daily-interview-question.

DangoSky avatar DangoSky commented on May 3, 2024 1
  var b = 10;
  (function b(){
    console.log(window.b);   // 打印10
    var b = 20;
    console.log(b);     // 打印20
  })();

from daily-interview-question.

yu910709 avatar yu910709 commented on May 3, 2024 1

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

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

from daily-interview-question.

Abiel1024 avatar Abiel1024 commented on May 3, 2024 1
var b = 10;
(function b(){
    b = 20;
    console.log(this.b); 
})();
// 10
var b = 10;
(function b(){
    let b = 20;
    console.log(b); 
})();
// 20

from daily-interview-question.

blly5 avatar blly5 commented on May 3, 2024 1
var b = 10; 
(function b(){ 
	let b = 20; 
	console.log(self.b, b); 
})(); 
//10、20

应该算比较少了吧😐

from daily-interview-question.

srrgithub avatar srrgithub commented on May 3, 2024 1

只改动一处。
10:
var b = 10;
(function b(){
b = 20;
console.log(10);
})();

20:
var b = 10;
(function b(){
b = 20;
console.log(20);
})();
没错我就是来搞笑的

from daily-interview-question.

icedJuice avatar icedJuice commented on May 3, 2024 1

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

from daily-interview-question.

Hideer avatar Hideer commented on May 3, 2024 1

image

from daily-interview-question.

cMYour avatar cMYour commented on May 3, 2024 1
// 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); 
})();

from daily-interview-question.

zengkaiz avatar zengkaiz commented on May 3, 2024 1
// 打印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);

from daily-interview-question.

LoveFreedom97 avatar LoveFreedom97 commented on May 3, 2024 1

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

from daily-interview-question.

lsczy avatar lsczy commented on May 3, 2024 1
var b = 10; 
(function b(b) {
   b = 20;
   console.log(window.b) // 10
   console.log(b)        // 20
})(b)

from daily-interview-question.

LaoLeo avatar LaoLeo commented on May 3, 2024

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

from daily-interview-question.

y1324 avatar y1324 commented on May 3, 2024

同时打印10 和20

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

from daily-interview-question.

huanglvming avatar huanglvming commented on May 3, 2024
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,为什么输出结果不一样?

from daily-interview-question.

y1324 avatar y1324 commented on May 3, 2024
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只是在作用于函数里面。

from daily-interview-question.

mengfei-nie avatar mengfei-nie commented on May 3, 2024

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

from daily-interview-question.

jjeejj avatar jjeejj commented on May 3, 2024

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

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

from daily-interview-question.

hmmoshang avatar hmmoshang commented on May 3, 2024

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

from daily-interview-question.

frjcxy avatar frjcxy commented on May 3, 2024

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

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

from daily-interview-question.

pangqiang avatar pangqiang commented on May 3, 2024
var b = 10;
(function b(b){
    console.log(b)
    b = 20;
    console.log(b);
})(b);

from daily-interview-question.

yu910709 avatar yu910709 commented on May 3, 2024

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

看第33题

from daily-interview-question.

qinnnn avatar qinnnn commented on May 3, 2024
var b = 10;
(function b(){
    var b = 20;
    console.log(this.b);
	console.log(b);
})();

from daily-interview-question.

kevinlorry avatar kevinlorry commented on May 3, 2024

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

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

from daily-interview-question.

witsaint avatar witsaint commented on May 3, 2024

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

from daily-interview-question.

kerrysheng avatar kerrysheng commented on May 3, 2024

这题太主观了,我可以一行不留的方式改造吗,🌹🐔

from daily-interview-question.

mongonice avatar mongonice commented on May 3, 2024

打印出10

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

打印出 20

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

from daily-interview-question.

liuchuandai avatar liuchuandai commented on May 3, 2024

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

我寻思着 这也改的不多啊

from daily-interview-question.

cheny-github avatar cheny-github commented on May 3, 2024
var b = 10;
(function b(b){
    console.log(b)
    b = 20;
    console.log(b); 
})(b);

from daily-interview-question.

houchaowei avatar houchaowei commented on May 3, 2024

看到好多都是先打印10,再打印20,题都理解错了,题目写的是分别打印10和20,是执行一次,打印两个数

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

from daily-interview-question.

bruser avatar bruser commented on May 3, 2024

有没有大神解释一下原理啊,很多修改跟直接写
console.log(10); console.log(20);
没什么区别

from daily-interview-question.

mouzhengjie avatar mouzhengjie commented on May 3, 2024

使用箭头函数也可实现:
打印10:
var b = 10;
( (b)=>{
this.b = 20;
console.log(b)
})(b);
打印20:
var b = 10;
( ()=>{
b = 20;
console.log(b)
})();

from daily-interview-question.

Croash avatar Croash commented on May 3, 2024

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

from daily-interview-question.

rzh11111111 avatar rzh11111111 commented on May 3, 2024

var b = 10;
(function b() {
b = 20;

    })(console.log(b))

//10 2333333(卖萌)

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

from daily-interview-question.

webbobola avatar webbobola commented on May 3, 2024

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

from daily-interview-question.

tal007 avatar tal007 commented on May 3, 2024

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

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

from daily-interview-question.

zhangyu0218 avatar zhangyu0218 commented on May 3, 2024

这个简单改造好像小时候玩的火柴拼公式。
------------------- 20 -------------------
var b = 10;
(function () {
b = 20;
console.log(b)
})()
------------------- 10 -------------------
var b = 10;
(function () {
b => 20;
console.log(b)
})()
不知道符不符合 哈哈~~

from daily-interview-question.

RimyZhang avatar RimyZhang commented on May 3, 2024

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

from daily-interview-question.

JohnieXu avatar JohnieXu commented on May 3, 2024
  1. 打印10
var b = 10;
(function b(){
    b = 20;
    console.log(window.b); 
})();
  1. 打印20
var b = 10;
(function b(){
    var b = 20;
    console.log(b); 
})();

from daily-interview-question.

JohnieXu avatar JohnieXu commented on May 3, 2024
  • 打印10
var b = 10;
(function b(){
    b = 20;
    console.log(window.b); 
})();
  • 打印20
var b = 10;
(function b(){
    var b = 20;
    console.log(b); 
})();

from daily-interview-question.

Jmingzi avatar Jmingzi commented on May 3, 2024
var b = 10;
(function b () {
    var b = 20
    console.log(window.b, b)  // 10, 20
})()

from daily-interview-question.

yygmind avatar yygmind commented on May 3, 2024
var b = 10;
(function b(){
    b = 20;
    console.log(b); 
})();

from daily-interview-question.

GitHubJiKe avatar GitHubJiKe commented on May 3, 2024
var b = 10;
(function b(){
    b = 20;
    console.log(this.b);//10 
})();

**********

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

from daily-interview-question.

yipjianbo avatar yipjianbo commented on May 3, 2024
console.log(10);
console.log(20);

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

from daily-interview-question.

yipjianbo avatar yipjianbo commented on May 3, 2024
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); 
})();

from daily-interview-question.

zhangqiong27 avatar zhangqiong27 commented on May 3, 2024

原题:
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

from daily-interview-question.

QZEming avatar QZEming commented on May 3, 2024

原题

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

from daily-interview-question.

jingsy avatar jingsy commented on May 3, 2024

有人把原题讲讲吗 为啥打印 [Function: b]

from daily-interview-question.

randomxiang avatar randomxiang commented on May 3, 2024
var b = 10;
(function b(b){
    console.log(b)
    var b = 20;
    console.log(b);
})(b);

from daily-interview-question.

zhangwen1932 avatar zhangwen1932 commented on May 3, 2024
  var b = 10;
 (function b() {
      let b=20;
      console.log(window.b);
      console.log(b);
 })();

from daily-interview-question.

codexu avatar codexu commented on May 3, 2024

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

from daily-interview-question.

soraly avatar soraly commented on May 3, 2024
var b = 10;
console.log(b);  //10
(function b(){
    var b = 20;
    console.log(b);  //20
})();

from daily-interview-question.

RoeyXie avatar RoeyXie commented on May 3, 2024

// 20

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

// 10

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

from daily-interview-question.

zelixag avatar zelixag commented on May 3, 2024

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

from daily-interview-question.

m7yue avatar m7yue commented on May 3, 2024
// 10
var b = 10;
(function b(){
    b = 20;
    console.log(window.b); 
})();

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

from daily-interview-question.

BlackCloverM avatar BlackCloverM commented on May 3, 2024

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

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

真把我逗笑了

from daily-interview-question.

BarneyRoos avatar BarneyRoos commented on May 3, 2024
//不太明白这题在考啥
var b = 10;
(function b(num){
    console.log(num)
    num = 20;
    console.log(num);
})(b);

from daily-interview-question.

wangsiyuan4 avatar wangsiyuan4 commented on May 3, 2024

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

from daily-interview-question.

Yangfan2016 avatar Yangfan2016 commented on May 3, 2024
// 打印 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)

from daily-interview-question.

weijuer avatar weijuer commented on May 3, 2024
// 10
var b = 10;
(function (){
    console.log(b); 
    b = 20;
})();

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

from daily-interview-question.

18920199681 avatar 18920199681 commented on May 3, 2024
var b = 10;
(function b(b){
    b = 20;
    console.log(this.b); 
    console.log(b); 
})(b);

from daily-interview-question.

liyi711 avatar liyi711 commented on May 3, 2024

打印20:

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

打印10:

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

from daily-interview-question.

XW666 avatar XW666 commented on May 3, 2024
 var b = 10;
  (function b() {
    var b = 20;
    console.log(b);
    console.log(this.b)
  })();

from daily-interview-question.

XW666 avatar XW666 commented on May 3, 2024
 var b = 10;
  (function b() {
    var b = 20;
    console.log(b);
    console.log(this.b)
  })();

from daily-interview-question.

Chenmin926 avatar Chenmin926 commented on May 3, 2024

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

from daily-interview-question.

npmrun avatar npmrun commented on May 3, 2024

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

from daily-interview-question.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.