面试1

1.为什么 typeof null  = 'object'

是通过类型标记位,而 null 是一个固定的值。开头为 000
000: object
001: integer
010: double
100: string
110: boolean
 
2. + 操作转化为数字
传统数据类型
toNumber(null)   0
toNumber(undefined)  NaN
toNumber(1)   1
toNumber("123aa")  NaN
toNumber({})  NaN
toNumber(true)  1
toNumber([])  0.  /. [0] = 0. /. [1] = 1.  /. [0, 1] = NaN

ES6的 bigInt和Symbol   报错
toNumber(10n)
toNumber(Symbol.for("a")). 
 
Symbol.for('1')  先找看有没有,有就返回,没有就新建,参数 哪怕是复杂数据类型 也是一样的
比如: Symbol( { a: 1, b: { c: 2 } } )
 
Symbol.keyFor( ss ) 参数是Symbol: 返回一个已登记的 值,如果是一个 复杂类型的 数据 返回 "[object Object]"
 
 
["1", "2", "3"].map(parseInt). 返回 [1, NaN, NaN]
当进制是 0 表示为 默认值 10,最小值为 2,如果 以 0X开头表示 16进制
 
in 操作符  可以判断 是否在对象中,或者原型对象中
 
 
== 判断的依据
null == 0 / false
'0' == false
1. 只要有一个是 boolean,先将  bool 转化为 数字
2. null 和 undefined 不进行转化
3. 2个操作数是对象的话,比较是否指向同一个对象
4. 有一个是字符串,另一个是数字,先将字符串转化为数字
5. 一个是对象,一个不是,需要先将其转化为基本类型,调用 valueOf() / toString
JS规定 undefined 和 null 相等,因为 undefined是从 null 衍生出来的
 
toString是把对象转换为字符串;
valueOf是把对象转换成一个基本数据的值
 
===严格比较运算符,不做类型转换,类型不同就是不等
Object.is 与 === 基本一致(不同 NaN 和 NaN 相等 /  +0 不等于 -0)
 
 
 
Object.getOwnPropertyDescriptor(globalThis, "undefined").  null 则返回 undefined
返回一个对象 writable / configurable / enumerable = false
 
typeof 判断不再 安全 可能会报错
 
String.prototype.constructor = function() {}. 可以被改写,但是是不生效的
 
为定义的变量。使用 typeof 不会 报错。instanceOf 会报错(右边必须是 函数或者 类
 
1 var frame = window.frames[0];
2 var isInstanceOf = [] instanceof frame.Array;
3 console.log("frame.Array", frame.Array);
4 console.log("isInstanceOf", isInstanceOf)  false

判断 类型

1.  使用 typeof  缺点:判断不出 Array 和 Object

2. 使用 instanceof. 

3. 使用 Object.prototype.toString.call(this, ...) 返回  [object, Object]

为什么会返回这个? 可以自定义

1 class MyArray {
2     get [Symbol.toStringTag](){
3         return "MyArray"
4     }
5 }
6 
7 var pf = console.log;
8 var a = new MyArray();
9 Object.prototype.toString.call(a)。返回 [object, MyArray]

 

Number.isNaN 和 isNaN 的区别

1. isNaN 会尝试转化为 数字,成功则 true,否则 false

2. Number.isNaN  直接判断

isNaN('aaa') // 会尝试将其转化为 数字。 true
Number.isNaN('aaa') // 不会转化       false
Number.isNaN = function (val) {
   return typeof val === "number" && isNaN(val);
};

 

千分位

/hello (?=[a-z]+)/.test("hello a")
function format_with_regex(number) {
    var reg = /\d{1,3}(?=(\d{3})+$)/g;
    return (number + '').replace(reg, '$&,');
}

 

Symbol.toPrimitive 的使用

let obj = {
  [Symbol.toPrimitive](hint) {
    switch (hint) {
      case 'number':
        return 123;
      case 'string':
        return 'str';
      case 'default':
        return 'default';
      default:
        throw new Error();
    }
  },
};
2 * obj; // 246
3 + obj; // '3default'
obj == 'default'; // true
String(obj); // 'str'

[].valueof()     得到 []

[].toString().   得到 ‘’

[2] + 2          得到 ‘22’

[] + { } = '[object Object]'. /. {} + [] = 0 / { } + { } = NaN. / [] + [] = ''

 

[][Symbol.ToPrimitive]  = undefined。 { } 也是一样

.valueof()  {}. /. .toString(). '[object Object]' 

 

对象输出的顺序: 

let obj = { 1: 'a', 2: 'b', 6: 'c', a: 'd', f: 'f' }

 

 

class Person {
    constructor(name, age){
        this.name = name
        this.age = age
    }
    
    getName = ()=> {
        return this.name
    }
    
    getAge(){
        return this.age
    }
}

getName 是在创建 对象里面,而 getAge在原型对象上

Object.hasOwnProperty。用于判断是否是本身就有
Object.getOwnPropertyDescriptor(obj, "name")。获取描述器
 
Object.defineProperty(obj, "name", { })  不写内容,默认里面 全是 false,所以 使用 obj.name = 1 无效
 
configurable:false 不能删除,也不能重新定义
enumerable: false 不能被 keys 遍历出来
writable: false  不能被修改

 

const obj = {. }

Object.defineProperty(obj, 'name', {. }) 默认得到的都是 false

 

Object.preventExtensions( obj ) 不能添加新属性,但是可以删除

Object.isExtensible(obj1)    是否可以扩展属性

Object.seal( obj ) 不能删除/增加属性,只能修改本属性。 和下面一样只对 第一层有效

Object.freeze( obj ) 同上,也不能修改本属性

Object.isFrozen( obj )  判断是 freeze 对象

 

Object.getPrototypeOf. 用于获取指定对象的原型。 null 和 undefined 报错
Object.setPrototypeOf(obj, null)。也会生效
 

Object.prototype.toString.call(Boolean.prototype). ==>. [object, Boolean]

 

Object.getOwnPropertyNames 包含可枚举属性和不可枚举属性,不包括 symbol
Object.ownKeys 包含所有属性,包括不可枚举属性和 symbol
for 循环 不能遍历出 不可枚举对象 / symbol 也不能

 

Object.create(Prototype) 创建一个对象,并且 obj.__proto__ = Prototype

Object.setPropertyOf(obj,  Prototype).  obj.__proto__ = Prototype

 

私有属性。#canBack = 123

手写 instanceof

function instanceOf(instance, cclass) {
  let proto = instance.__proto__;
  let prototype = cclass.prototype;

  while (proto) {
    if (proto === prototype) return true;
    proto = proto.__proto__;
  }
  return false;
}

对象不能 使用 for. of 遍历。obj[Symbol.iterator]

 
posted @ 2024-03-08 16:54  escapist  阅读(3)  评论(0编辑  收藏  举报