striveCode icon indicating copy to clipboard operation
striveCode copied to clipboard

算是面试题

Open itstrive opened this issue 8 years ago • 3 comments

var x = 1;

function func(a) {
	if (a) {
		var x = Math.random();
		return x;
	}
	return x;
}
console.log(func(false));  //答案是多少?为什么
let x = 1;

function func(a) {
	if (a) {
		let x = Math.random();
		return x;
	}
	return x;
}
console.log(func(false));  //答案是多少?为什么

itstrive avatar Jul 21 '17 10:07 itstrive

undefined 1 不要问我为什么

tengfeihan avatar Jul 21 '17 11:07 tengfeihan

undefined //作用域链 变量提升 1 // let块级作用域 作用域链

Goldbeener avatar Jul 21 '17 14:07 Goldbeener

  • 分别用ES5和ES6找出数组最大值?
// ES5:
> Math.max.apply(Math, [-10,90,300,20])
// ES6:
> Math.max(...[-10,90,300,20])
  • 分别用ES5和ES6把其中一个数组的值添加到另一个数组中?
// ES5:
var arr1=[1,2];
var arr2=[3,4];
arr1.push.apply(arr1,arr2);  //arr1 -> [1,2,3,4]

// ES6:
const arr1=[1,2];
const arr2=[3,4];
arr1.push(...arr2);  //arr1 -> [1,2,3,4]
  • 分别用ES5和ES6的语法把多个数组拼接成一个数组?
// ES5:
var arr1=['a','b'];
var arr2=['c'];
var arr3=['d','e'];
console.log(arr1.concat(arr2,arr3));

// ES6:
const  arr1=['a','b'];
const  arr2=['c'];
const  arr3=['d','e'];
console.log([...arr1, ...arr2, ...arr3]);

itstrive avatar Aug 09 '17 03:08 itstrive