JavaScript 好题汇总分享(持续更新,看到好题就写)

前言

本文搜集的题目,均来自各大网课、面试题等

预编译相关

1

a = 100;
function demo(e) {
	function e() {  }
	arguments[0] = 2;
	console.log(e);
	if(a){
		var b = 123;
		function c() {
		}
	}
	var c;
	a = 10;
	var a;
	console.log(b);
	f = 123;
	console.log(c);
	console.log(a);
}
var a;
demo(1);
console.log(a);
console.log(f);

2

function bar(){
	return foo;
	foo = 100;
	function foo() {}
	var foo = 111;
}
console.log(bar()); 
console.log(bar());
function bar() {
	foo = 10;
	function foo() {}
	var foo = 11;
	return foo;
}

类型转化相关

if (typeof (a) && -true + (-undefined) + "") {
		document.write("基础扎实");
}
if(11 + "11" * 2 == 33){
	document.write("基础扎实");
}
!!" " + !!"" - !!false || document.write('你觉得能打印,你就是猪');

闭包

function a() {
	var arr = [];
	for (var i = 0; i < 10; i++) {
		arr[i] = function () {
			document.write(i + " ");
		}
	}
	return arr;
}
var myarr = a();
for (var i = 0; i < 10; i++) {
	myarr[i]();
}
function a() {
	var arr = [];
	for (var i = 0; i < 10; i++) {
		(function (j) {
			arr[j] = function () {
				document.write(j + " ");
			}
		}(i))
	}
	return arr;
}
var myarr = a();
for (var i = 0; i < 10; i++) {
	myarr[i]();
}

原型

Person.prototype.name = 'Jack';
function Person() {

}
person = new Person();
Person.prototype.name = 'Cherry';
console.log(person.name);
Person.prototype.name = 'Jack';
function Person() {

}
person = new Person();
Person.prototype = {
	name: 'Cherry'
}
console.log(person.name);
function Person() {

}
Person.prototype.name = 'Jack';
Person.prototype = {
	name: 'Cherry'
}
person = new Person();
console.log(person.name);

this

var name = '111';
var a = {
	name: '222',
	say: function () {
		console.log(this.name);
	}
}
var fun = a.say;
fun();
a.say();

var b = {
	name: '333',
	say: function (fun) {
		fun();
	}
}
b.say(a.say);
b.say = a.say;
b.say();
var foo = '123';

function f() {
	this.foo = '234';
	console.log(foo);
}
new f();

数组

function addTolist(item, list) {
	return list.push(item);
}
var result = addTolist('apple', ['banana']);
console.log(result);
//类数组
var obj = {
	'2': 'a',
	'3': 'b',
	'length': 2,
	'push': Array.prototype.push
}
obj.push('c');
obj.push('d');
console.log(obj);

暂时未知类型

(window.x || (window.x = "bar")); 
console.log(window.x);
function a() {
	console.log('a');
}();          //报错
function a(a, b, c, d) {
		console.log(a + b + c + d);
}(1, 2, 3, 4);//不报错,但是不执行
var x = 1;
if (function a() {}) {
	x += typeof (a);
}
console.log(x);
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值