前言
本文搜集的题目,均来自各大网课、面试题等
预编译相关
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);