码农知识堂 - 1000bd
  •   Python
  •   PHP
  •   JS/TS
  •   JAVA
  •   C/C++
  •   C#
  •   GO
  •   Kotlin
  •   Swift
  • 原型、原型链、判断数据类型


    目录

    作用:共享属性

    原型链:obj.__proto__===Obj.prototype

    引用类型:__proto__(隐式原型)属性,属性值是对象

    函数:prototype(原型)属性,属性值是对象

     null:唯一不从 Object.prototype 继承的对象(防止原型污染攻击)

     map 的简单替代品

    Object.prototype 的原型始终为 null 且不可更改

    Function.prototype === Function.__proto__; // true

    Function本身也是函数,所以 Function是Function的实例

    应用

    函数装饰器:在一个函数 执行前 或 执行后 添加额外的逻辑

    Object

    Object.prototype 方法:多态

    Object静态方法/Object.prototype.方法.call(obj,...)

    instanceof代替Obj.prototype.isPrototypeOf(obj)

    包含本身的类型,还有继承的父类型帕努单

    Obj.get/setPrototypeOf(obj)代替obj._ _ proto _ _ 

    Obj.hasOwn(obj, prop)代替Obj.prototype.hasOwnProperty()

    Object.assign(target, ...sources)返回修改后的同地址的obj(target)

    对象内容,而非地址Object.assign({}, obj)浅拷贝一层

    Object.is(val0, val1):NaN等,±0不等

    ===、==:NaN不等、±0等

    Obj.create(obj[,props]) 以实例作为原型,创建新对象

    Object.defineProperties(obj,props)

    数据描述符

    configurable:默认false,不可删改

    enumerable:默认为 false,​不可枚举(自有属性​)

    Object.prototype.propertyIsEnumerable(prop)

    等价于 Object.getOwnPropertyDescriptor(obj, prop)?.enumerable ?? false

    value: 默认 undefined

    writable:默认false,不能用赋值运算符修改

    访问器描述符

    get:默认undefined,函数返回值将被用作属性的值

    set:默认undefined

    arr:Object.keys()/values()

    运算符

    delete obj.property

    delete obj[property]

    property in obj

    自身和继承

    all:in操作符

    可枚举:for in

    自身可枚举:Object.keys

    props:propertyNames

    props+symbol:propertyDescripts


    js判断数据类型、toString和valueOf区别,类型转换、不同类型间的运算、判断相等

    作用:共享属性

    原型:提供继承者共享属性/方法的对象

    原型链:obj.__proto__===Obj.prototype

    引用类型:__proto__(隐式原型)属性,属性值是对象


    函数:prototype(原型)属性,属性值是对象

    1. const o = {
    2. a: 1,
    3. b: 2,
    4. // __proto__ 设置了 [[Prototype]]。它在这里被指定为另一个对象字面量。
    5. __proto__: {
    6. b: 3,
    7. c: 4,
    8. __proto__: {
    9. d: 5,
    10. },
    11. },
    12. };
    13. // { a: 1, b: 2 } ---> { b: 3, c: 4 } ---> { d: 5 } ---> Object.prototype ---> null
    14. console.log(o.d); // 5

     null:唯一不从 Object.prototype 继承的对象(防止原型污染攻击)

    1. const user = {};
    2. // 恶意脚本:
    3. Object.prototype.authenticated = true;
    4. // 意外允许未经身份验证的用户通过
    5. if (user.authenticated) {
    6. // 访问机密数据
    7. }

     map 的简单替代品

    1. const ages = { alice: 18, bob: 27 };
    2. function hasPerson(name) {
    3. return name in ages;
    4. }
    5. function getAge(name) {
    6. return ages[name];
    7. }
    8. hasPerson("hasOwnProperty"); // true
    9. getAge("toString"); // [Function: toString]

    由于存在 Object.prototype 属性,会导致一些错误:

    1. const ages = Object.create(null, {
    2. alice: { value: 18, enumerable: true },
    3. bob: { value: 27, enumerable: true },
    4. });
    5. hasPerson("hasOwnProperty"); // false
    6. getAge("toString"); // undefined

    Object.prototype 的原型始终为 null 且不可更改

    Function.prototype === Function.__proto__; // true

    Function本身也是函数,所以 Function是Function的实例

    应用

    函数装饰器:在一个函数 执行前 或 执行后 添加额外的逻辑

    1. Function.prototype.before=function(beforefn){
    2. return ()=>{
    3. beforefn.apply(this,arguments)
    4. return this.apply(this,arguments)
    5. }
    6. }
    7. Function.prototype.after=function(afterfn){
    8. return ()=>{
    9. var res=this.apply(this,arguments)
    10. afterfn.apply(this,arguments)
    11. return res;
    12. }
    13. }
    14. var func=function(){
    15. console.log(1)
    16. }.before(function(){
    17. console.log(2)
    18. }).after(function(){
    19. console.log(3)
    20. })
    21. func()//213

    Object

    Object.prototype 方法:多态

    应该避免调用任何 Object.prototype 方法,特别是那些不打算多态化的方法(即只有其初始行为是合理的,且无法被任何继承的对象以合理的方式重写),而尽量用Object的静态方法

    Object静态方法/Object.prototype.方法.call(obj,...)

    如果不存在语义上等价的静态方法,或者你真的想使用 Object.prototype 方法,你应该通过 call()直接在目标对象上调用 Object.prototype 方法,以防止因目标对象上原有方法被重写而产生意外的结果。

    1. const obj = {
    2. foo: 1,
    3. // 如果可能的话,你不应该在自己的对象上定义这样的方法,
    4. // 但是如果你从外部输入接收对象,可能无法防止这种情况的发生
    5. propertyIsEnumerable() {
    6. return false;
    7. },
    8. };
    9. obj.propertyIsEnumerable("foo"); // false;预期外的结果
    10. Object.prototype.propertyIsEnumerable.call(obj, "foo"); // true;预期的结果

    instanceof代替Obj.prototype.isPrototypeOf(obj)

    包含本身的类型,还有继承的父类型帕努单

    Obj.get/setPrototypeOf(obj)代替obj._ _ proto _ _ 

    Obj.hasOwn(obj, prop)代替Obj.prototype.hasOwnProperty()

    在支持 Object.hasOwn 的浏览器中,建议使用 Object.hasOwn(),而非 hasOwnProperty()。

    1.与重写的hasOwnProperty一起使用

    1. const foo = {
    2. hasOwnProperty() {
    3. return false;
    4. },
    5. bar: "The dragons be out of office",
    6. };
    7. if (Object.hasOwn(foo, "bar")) {
    8. console.log(foo.bar); //true——重新实现 hasOwnProperty() 不会影响 Object
    9. }

    2.测试使用 Object.create(null) 创建的对象。这些对象不会继承自 Object.prototype,因此 hasOwnProperty() 方法是无法访问的。

    1. const foo = Object.create(null);
    2. foo.prop = "exists";
    3. if (Object.hasOwn(foo, "prop")) {
    4. console.log(foo.prop); //true——无论对象是如何创建的,它都可以运行。
    5. }

    Object.assign(target, ...sources)返回修改后的同地址的obj(target)

    1. const target = { a: 1, b: 2 };
    2. const source = { b: 4, c: 5 };
    3. const returnedTarget = Object.assign(target, source);
    4. console.log(target);
    5. // Expected output: Object { a: 1, b: 4, c: 5 }
    6. console.log(returnedTarget === target);
    7. // Expected output: true
    对象内容,而非地址Object.assign({}, obj)浅拷贝一层
    1. let obj1 = { key: 'value' };
    2. let obj2 = Object.assign({}, obj1); // 创建obj1的浅拷贝
    3. obj1.key = 'new value';
    4. console.log(obj2.key); // 输出: 'value'

    Object.is(val0, val1):NaN等,±0不等

    严格值相等

    ===、==:NaN不等、±0等

    Obj.create(obj[,props]) 以实例作为原型,创建新对象

    props等价于Object.defineProperties的props

    Object.defineProperties(obj,props)

    1. const object1 = {};
    2. Object.defineProperties(object1, {
    3. property1: {
    4. value: 42,
    5. writable: true,
    6. },
    7. property2: {},
    8. });
    9. console.log(object1.property1);
    10. // Expected output: 42
    数据描述符
    configurable:默认false,不可删改
    enumerable:默认为 false,​不可枚举(自有属性​)

    大多数内置属性默认情况下是不可枚举的

    Object.prototype.propertyIsEnumerable(prop)
    等价于 Object.getOwnPropertyDescriptor(obj, prop)?.enumerable ?? false

    ?? 是空值合并操作符(Nullish Coalescing Operator)

    左侧为 null 或者 undefined 时,返回其右侧,否则返回左侧

    value: 默认 undefined
    writable:默认false,不能用赋值运算符修改
    访问器描述符
    get:默认undefined,函数返回值将被用作属性的值
    set:默认undefined

    arr:Object.keys()/values()

    运算符

    delete obj.property


    delete obj[property]

    试图删除的属性不存在,那么 delete 将不会起任何作用,但仍会返回 true

    property in obj

    1. const trees = ["redwood", "bay", "cedar", "oak", "maple"];
    2. trees[3] = undefined;
    3. console.log(3 in trees); // true

    自身和继承

    all:in操作符

    可枚举:for in

    自身可枚举:Object.keys

    props:propertyNames

    props+symbol:propertyDescripts

    Object.getOwnPropertyDescriptor(obj, prop)

  • 相关阅读:
    ansible copy 模块
    FFmpeg开发笔记(三十八)APP如何访问SRS推流的RTMP直播地址
    相机内参模型Mei/omni-directional详解
    利用改进的YOLOv5模型对玉米和杂草进行精准检测和精准喷洒
    Go语学习笔记 - gorm使用 - 数据库配置、表新增 Web框架Gin(七)
    cuda 核函数的定义和使用
    【Linux系统管理】09 文件系统管理 & 高级文件系统管理
    msvcr120.dll丢失是什么意思,快速修复msvcr120.dll丢失的问题的方法分享
    R语言随机波动模型SV:马尔可夫蒙特卡罗法MCMC、正则化广义矩估计和准最大似然估计上证指数收益时间序列...
    腾讯RPC框架开源了
  • 原文地址:https://blog.csdn.net/qq_28838891/article/details/133136691
  • 最新文章
  • 攻防演习之三天拿下官网站群
    数据安全治理学习——前期安全规划和安全管理体系建设
    企业安全 | 企业内一次钓鱼演练准备过程
    内网渗透测试 | Kerberos协议及其部分攻击手法
    0day的产生 | 不懂代码的"代码审计"
    安装scrcpy-client模块av模块异常,环境问题解决方案
    leetcode hot100【LeetCode 279. 完全平方数】java实现
    OpenWrt下安装Mosquitto
    AnatoMask论文汇总
    【AI日记】24.11.01 LangChain、openai api和github copilot
  • 热门文章
  • 十款代码表白小特效 一个比一个浪漫 赶紧收藏起来吧!!!
    奉劝各位学弟学妹们,该打造你的技术影响力了!
    五年了,我在 CSDN 的两个一百万。
    Java俄罗斯方块,老程序员花了一个周末,连接中学年代!
    面试官都震惊,你这网络基础可以啊!
    你真的会用百度吗?我不信 — 那些不为人知的搜索引擎语法
    心情不好的时候,用 Python 画棵樱花树送给自己吧
    通宵一晚做出来的一款类似CS的第一人称射击游戏Demo!原来做游戏也不是很难,连憨憨学妹都学会了!
    13 万字 C 语言从入门到精通保姆级教程2021 年版
    10行代码集2000张美女图,Python爬虫120例,再上征途
Copyright © 2022 侵权请联系2656653265@qq.com    京ICP备2022015340号-1
正则表达式工具 cron表达式工具 密码生成工具

京公网安备 11010502049817号