1.toString() 结果是多少?
报错“Invalid or unexpected token”,这是因为 JavaScript
解析器试图将点操作符解析为浮点数字面值的一部分。可以使用下面写法:
1..toString()
1 .toString()
(1).toString()
下面代码结果多少?
第一个 NaN,var a = 1 导致 a 在 fn 里面声明提升了, 相当于 var a,但是未赋值,所以是 undefined,执行 a++
的时候会变成 NaN。
第二个 1,因为赋值了 a = 1。
var a = 1
function fn() {
a++
console.log(a)
var a = 1
console.log(a)
}
fn()
下面代码结果多少?
错误“not defined”,命名函数表达式(named function
expression),函数名称只能在函数体内部(作用域)引用。
const f = function fn(){ console.log(1) }
fn()
下面代码结果多少?
2,函数声明会提前(hoisted),函数表达式因变量定义,声明提前但赋值因 return 不会执行到。
console.log(fn())
function fn() {
return 1
}
function fn() {
var test = function() {
return 2
}
return test()
var test = function() {
return 3
}
}
下面代码结果多少?
5,3,4,5,6,undefinded。函数执行时,arguments 对象为实参相应的形参创建 getter 和 setter
方法,无则为 undefined。因此,改变形参的值会影响到 arguments 对象的值,反之亦然。通过删除属性值并重定义或变成一个可访问属性时,可断开关联。
function fn(a, b, c) {
a = 2
b = 3
c = 4
delete a
arguments[0] = 5
delete arguments[1]
arguments[1] = 6
return [a, b, c, arguments[0], arguments[1], arguments[2]]
}
console.log(fn(1, 1))
下面代码结果多少?
2,3。严格模式下要求函数参数是唯一的,原先的同名参数通过 arguments[i] 只是简单拷贝传入函数的值,不再动态关联属性值和形参值。
function fn(a) {
'use strict'
a = 2
arguments[0] = 3
return [a, arguments[0]]
}
console.log(fn(1))
下面代码结果多少?
F对象,Object对象,F对象。所有的对象都有一个原型属性(prototype),指向通过构造函数创建的那个函数的原型对象,原型对象中有一个 constructor 属性指向包含 prototype
属性的函数,构造函数的实例包含一个内部属性指向原型对象(Chrome是__proto__),当我们访问一个对象中的属性时,首先会询问实例对象中有没有该属性,如果没有则继续查找原型对象。
function F() {}
var f = new F()
F.prototype.fn = function test() {}
console.log(f.constructor)
F.prototype = {}
f = new F()
console.log(f.constructor)
F.prototype = {
constructor: F
}
f = new F()
console.log(f.constructor)
下面代码结果多少?
false。join 依赖 toString,[10,1] 类型转化后是 'a'。
Array.prototype.toString = function() {
return 'a'
}
Array.prototype.join = function() {
return 'b'
}
console.log([10,1] == '10,1')
下面代码结果多少?
o1。this 指向最后一个调用该函数的对象。
function fn() {
console.log(this)
}
const o1 = {
x: fn
}
const o2 = {
x: o1
}
o2.x.x()
下面代码结果多少?
false。构造函数 F 中,this 指向实例对象,但函数 f 声明调用中,this 指向全局对象
function F() {}
F.prototype.test = function() {
function fn() {
console.log(this == a)
}
fn()
}
new F().test()
下面代码结果多少?
true,false。方法调用中,this 指向方法的对象,但把方法赋值给变量a时,又是普通的函数调用。
const f = {
x: function() {
console.log(this == f)
}
}
f.x()
const a = f.x
a()
下面代码结果多少?
false,true。hasOwnProperty 并不受保护,同名时,可以通过外部的 hasOwnProperty 来获取正确结果。
const o = {
hasOwnProperty: function() {
return false;
},
a: 'xx'
}
console.log(o.hasOwnProperty('a'))
console.log(({}).hasOwnProperty.call(o, 'a'))
下面代码结果多少?
3,数组是一种数值(下限0)索引的map。数组元素是对象属性,数值索引是一个字符型,而不是数字,当用数字下标符访问时,
会被转换成字符型。length数值上始终大于每个可索引属性名。不支持关联数组。Understanding
JavaScript Arrays
const a = []
a[0] = 'a'
a[1] = 'b'
a['2'] = 'c'
a[-1] = 'd'
a.x = 'e'
console.log(a.length)
下面代码结果多少?
false,length赋值小于数组长度时,数组中索引大于等于新赋值的元素将别丢弃。
const a = [1, 2, 3]
a.length = 2
console.log('2' in a)
下面代码结果多少?
1,函数声明提前于var声明。
var f = 1
function fn() {
f = 2
return
function f() {}
}
fn()
console.log(f)
下面代码结果多少?
2,label声明, 等效 {a: 1}; +'2'。
{a: 1} + '2'
下面代码结果多少?
报错“Unexpected token ':'”,逗号后为表达式。
{a: 1, b: 2}
下面代码结果多少?
true,等效 [true, false][0]
[true, false][+true, +false]
下面代码结果多少?
undefined, F,严格模式下,未绑定函数的上下文默认是 undedined。
'use strict'
function F(){
console.log(this)
}
F()
new F()
下面代码结果多少?
window,bind 返回一个新函数并绑定到指定参数对象。
document.body.addEventListener('click', f.bind(this))
function f() {
console.log(this)
}
下面代码结果多少?
报错“Cannot convert object to primitive value”, 这样创建的对象无原型链,所以没有 toString方法。
const a = Object.create(null)
console.log(a + '')
下面代码结果多少?
1,NaN。apply 传入类数组对象时,需要 length 属性和正整数属性范围,注意浏览器实现对参数数量有上限。Math.min
如果任一参数不能转换为数值,则返回 NaN,如果没有提供参数,返回 Infinity。
Math.max.apply(null, {'0':1, '1':2, length:1})
Math.min.apply(null, {'0':1, '-1':-1, length:2})
下面代码结果多少?
'12.3450000000000006394885',报错“toFixed() digits argument must be between 0
and 100”。toFixed的数字建议0~20,具体视浏览器实现而定,不一定报酬
const a = 12.345
a.toFixed(22)
a.toFixed(-1)
下面代码结果多少?
1,NaN,1。parseInt 会忽略串首空格,非字符会先调 toString 转成字符,010 默认转成'8'。
parseInt(' 01 2')
parseInt(010, 8)
parseFloat('1.0')
下面代码结果多少?
-1, 1。indexOf 第二个参数为起始值,函数 length 只计算第1个有默认值之前的参数个数。
console.log([1,2,3].indexOf(1,2))
console.log((function(a, b = 1, c) {}).length)
下面代码结果多少?
number,number,function。变量和函数声明都会在当前作用域提前,IIFE 不影响赋值
var a = 1
console.log(typeof a)
(function a () {})()
console.log(typeof a)
a = function a() {}
console.log(typeof a)
下面代码结果多少?
true。includes 使用的是 SameValueZero 方式比较。
[NaN].includes(NaN)
下面代码结果多少?
true,false,true,false。超出安全范围 Number.MIN_SAFE_INTEGER ~
Number.MAX_SAFE_INTEGER 的整数计算都可能是不精确的。
const a = Number.MAX_SAFE_INTEGER
const b = Number.MIN_SAFE_INTEGER
console.log(a + 1 == a + 2)
console.log(a + 1 == a + 3)
console.log(b - 1 == b - 2)
console.log(b - 1 == b - 3)
下面代码结果多少?
bigint,true,false,TypeError。如果不支持 BigInt,均报错“Cannot mix BigInt and other
types”。
console.log(typeof 12n)
console.log(12n == 12)
const a = BigInt(Number.MAX_SAFE_INTEGER)
console.log(a + 1n == a + 2n)
console.log(a + 1)
下面代码结果多少?
['一', '二'],['一', '、', '二']。分隔符使用了包含捕获括号的正则表达式,则分隔符包含在结果中。
const str = '一、二'
console.log(str.split('、'))
console.log(str.split(/(、)/))
下面代码结果多少?
2,{x: 2}。按求值操作,a.x 方式是先求左边值,此时 a.x 是对再次赋值前 {x: 1} 中 x
的引用,等效 b.x,之后 a 被重新赋值。
2,{x: {x: ...}} 循环引用。a.x 是引用 a 本身。
2,[2],数组类似对象。
求值 evaluation
和属性访问器 Property Accessors
var a = {x: 1}
var b = a
a.x = a = {x: 2}
console.log(a.x)
console.log(b.x)
var a = {x: 1}
var b = a
a.x = a
a = {x: 2}
console.log(a.x)
console.log(b.x)
var a = [1]
var b = a
a[0] = a = [2]
console.log(a[0])
console.log(b[0])
下面代码结果多少?
null,1。bind 操作相当于对传入匿名函数做了一次 apply 操作,null 做为 thisArg(严格模式下不会替换成全局对象),{ 0: 1,
length: 1}为类数组参数。
'use strict'
Function.apply.bind(function(x) {
console.log(this, x)
}, null)({ 0: 1, length: 1 })
下面代码结果多少?
1,false。promise 会调用.then方法,如果声明则调用声明的 then 方法,动态 import 返回一个 promise,这样,模块加载后触发 resolve,从而直接调用声明的
then。这种非预期情形,见历史讨论 tc39
issue47、tc39 issue48。
一种方式是增加一些修正,让 Promise.resolve 避免执行 thenable 行为。
Promise.resolve({
[Symbol.thenable]: false,
then() {
return '1'
},
}).then((o) => {
o.then() === '2'
})
// t.js
export async function then(r) {
r()
console.log(1)
}
import * as t1 from './t.js'
import('./t.js').then((t2) => {
console.log(t1 === t2)
})