第一题
var product = 'apple';
var obj = {
produc:'pen',
prop:{
product:'apple pen',
getProduct:function(){
return this.product;
}
}
}
product='pineapple';
console.log(obj.prop.getProduct());
var buy = obj.prop.getProduct;
console.log(buy());
======执行结果为
=========================apple pen
=========================pineapple
第二题 考察箭头函数
window.name = 'A'
const obj = {
name:'B',
getName:()=>{
console.log(this.name);
console.log(this);
}
}
const fn = obj.getName;
fn();
obj.getName();
fn.call({name:'c'})
=====================执行结果为 A A A
第三题 考察arguments对象里是实参值
function test(a,b){
console.log(arguments)
return arguments+b+arguments[2];
}
var result = test('a','b','c','d');
console.log(result)
第四题
var a = { x:1 };
var b = a;
a = a.x={x:1};
console.log(a)
console.log(b)
第五题
var num = 1;
function myFunc(){
alert(num);
var num =2;
alert(num);
}
myFunc();
第六题
var v1 = 'global';
var f1 = function(){
console.log(v1);
};
f1();
var f2 = function(){
var v1 = 'local';
f1();
};
f2();
第七题 ??
var name = "global";
var obj = {
name:"local",
foo:function(){
this.name = 'foo';
}.bind(window)
};
var bar = new obj.foo();
setTimeout(function(){
console.log(window.name)
},0)
console.log(bar.name);
var bar3=bar2=bar;
bar2.name = 'foo2';
console.log(bar3.name);
第八题 题目有问题
var objName = " name1";
function obj(){
var objName = " name2";
funciton innerObj(){
alert(objName);
}
innerObj();
}
alert(obj());
第九题
window.a = 300;
function fn1(){
this.a = 100;
this.b = 200;
return function(){
alert(this.a)
}.call(arguments[0]);
}
function fn2(){
this.a = new fn1();
}
var a = new fn1().b;
var v = new fn1(fn2());
分别弹出 300 200 [object Object]
第十题 ??
function Dog(){}
Dog.prototype = {
bark(){
console.log(1)
}
}
const dog = new Dog()
dog.bark()
Dog.prototype = {
bark(){
console.log(2)
}
}
dog.bark()
Dog.prototype = {
bark(){
console.log(3)
}
}
dog.bark()
输出三个 1
第十题
let num = 10;
let output = '';
for(let i=1;i<=num;i++){
let sqRoot = Math.sqrt(i);
if(Math.floor(sqRoot)!==sqRoot){
continue;
}
output+=i+'';
}
console.log(output);
第十一题
const name ='name';
const test = `${name}`"${name}"`${name}`;
console.log(test);
const name = 'name';
const test = {name};
console.log(test)
第十二题
"use strict";
a = 1;
var a = 2;
console.log(window.a);
console.log(a);
第十三题
var {a,b,c}={"c":10,"b":9,"a":8}
console.log(a)
console.log(b)
console.log(c)
第十四题
console.log(NaN==false)
console.log(!NaN==false)
console.log(!!NaN==false)
console.log(NaN==true)
console.log(!NaN==true)
console.log(!!NaN==true)
function test(a,b){
return a+b+arguments[2];
}
var result = test("a","b","c","d")
console.log(result);
第十六题
var a = new Date(2020,3,8)
console.log(a)