til
  • README
  • Software Development Roles
  • solid
  • README
    • service-worker
  • docker
    • arg
    • 更新docker版本
  • editor
    • vscode
    • Creating a VS Code Theme
  • english
    • words
  • front-end
    • ==
    • ECMAScript
    • IIFE
    • Label
    • basic
    • html.js.css渲染顺序
    • npm-vs-yarn
    • obj-delete-key-value
    • react
    • split-join-and-replace
    • video
    • 前端自检清单
    • 递归及去重
    • css
      • css换肤
      • flex
      • list
      • nth-child和nth-of-type区别
      • padding
      • position
      • 层叠上下文
      • 层叠样式(+)
      • 正方形
      • 语义化标签
    • dom
      • DOCTYPE
      • HEAD
      • 修改document
      • 自定义表单验证
    • electron
      • basic
    • es6
      • basic-type
      • basic
      • prototype-example
      • defineProperty
      • understanding-es6
        • 0.introduction
        • Appendix A: Smaller Changes
        • Appendix B: Understanding ES7
        • Block-Binding
        • Proxies&Reflection
        • class
        • 解构赋值
        • function
        • improved-array
        • iterators&generators
        • modules
        • object
        • promise
        • Map&Set
        • symbol
    • images
      • 前端角度看图片
    • interview_case
      • lexical_scope
      • redux和localstroage存储位置
    • javascript
      • fuck-the-js
      • js-engine-work
      • js原生操作dom
      • what-is-function-program
      • 执行上下文
      • articles
        • JavaScript中使用函数组合
        • JavaScript中的依赖注入
        • JavaScript作用域链中的标识符解析和闭包
        • JavaScript是何如工作的--概述
        • JavaScript深拷贝
        • JavaScript的全局变量是如何工作的
        • js继承常见的误解
        • node12&chrome中7个新的提案功能
        • 你真的懂JavaScript吗
      • date
        • index
      • engines
        • basic
        • JavaScript引擎基础:外形和内联缓存
        • v8中推测性优化的介绍
        • 优化原型
        • 更快的异步功能和promise
      • events
        • baisc
        • 事件冒泡和捕获
        • 定义事件
        • 页面生命周期
      • higher-order-function
        • curry
        • monad
      • module
        • basic
        • main&module
      • objects
        • iterator
        • spread
        • examples
          • iterator
      • performance
        • blocking-css
        • cache
      • prototype
        • Property-Descriptors
        • basic
        • prototype-shadow
      • you-dont-known-js
        • async&performance
          • Chapter 1: Asynchrony: Now & Later
          • Chapter 2: Callbacks
          • Chapter 3: Promises
          • Chapter 4: Generators
        • scope & closures
          • apA
          • apB
          • apC
          • apD
          • chapter1-what-is-scope
          • chapter2-lexical-scope
          • chapter3-function-vs-block-scope
          • chapter4-hoisting
          • chapter5-scope-closure
        • this & object prototypes
          • chapter1-this-or-that
          • chapter2-this-make-sense
          • chapter3-objects
          • chapter4-mixing(up)-class-object
          • chapter5-prototype
          • chapter6-behavior-delegation
        • types&grammer
          • Chapter1-Types
          • Chapter2-Values
          • Chapter3-Natives
          • Chapter4-coercion
          • Chapter5-grammer
        • up & going
          • chapter1-into-programming
          • chapter2-into-javascript
          • chapter3-into-YDKJS
    • mobile
      • iPhone分辨率终极指南
    • npm
      • arguments
      • build
    • react-native
      • prop-methods
    • react
      • PropTypes
      • basic
      • codebase-overview
      • component-element-instance
      • context
      • how-to-known-component-is-func-or-class
      • overview
      • react16.9
      • react18计划
      • react的设计原则
      • reconciliation
      • setState
      • useMemo
      • why-do-we-write-super-props
      • 从头实现一个react
      • concurrent
        • 引入并发模式(仅试验)
      • conf
        • conf-2019
      • events
        • 合成事件概述
      • hooks
        • custom-hook
        • effect-hook
        • hooks-api
        • intro
        • overview
        • rules
        • state-hook
        • hooks-vs-class
          • thinking-in-react-hooks
      • overreact
        • Development模式是如何工作的
        • How-Does-setState-Know-What-to-Do
        • Why-Do-React-Elements-Have-a-$$typeof-Property
        • Why-Do-React-Hooks-Rely-on-Call-Order
        • how-to-known-component-is-func-or-class
        • preparing-tach-talk-motivation
        • react作为ui运行
        • things-i-dont-known-as-2018
        • ui-element-problem-and-build-yourself
        • why-do-we-write-super-props
        • 一份完整的useEffect指南
        • 为什么X不是Hook
        • 函数组件与类有什么不同?
        • 演讲准备2-what-why-how
        • 编写弹性组件
        • 让setInterval在React-Hooks中为声明式
      • practice
        • render
      • react-dom
        • basic
      • react-redux
        • apiv7.1-hooks
        • connect
        • shallow-equal
      • redux
        • applyMiddleware
        • applyMiddleware2-细节
        • example
    • regex
      • index
    • stories
      • 数组下标
      • 阻止事件冒泡
    • svelte
      • compile-svelte-in-your-head-1
      • compiler-overview
      • parser
        • 写一个解析器-JavaScript的JSON解析器
    • turbopack
      • basic
    • typescript
      • interface和type的区别
    • webpack
      • hash
      • webpack4-for-react
      • webpack4
      • webpack4to5
      • babel
        • babel-parser和acorn的区别
        • babel.7.11
        • family
        • react16.14使用new-transform
        • update-to-7
    • pdf
      • deep-js
        • basic
      • react
        • reintroducing
  • git
    • capital
    • emoji
  • http
    • http2.0
    • response
  • rails
    • api
    • flash
    • middleware-vs-metal
    • model
    • performance
    • routes
    • environment
      • error
    • patterns
      • service
    • sidekiq
      • params
    • deploy
      • capistrano
        • ssh
  • ruby
    • self
    • net
      • http请求携带cookie
  • server
    • ss
    • ssh
    • user
    • crawler
      • puppeteer
    • nginx
      • domain-without-80
      • nginx节省带宽
  • sql
    • rails
    • search
Powered by GitBook
On this page
  1. front-end
  2. es6

defineProperty

Previousprototype-exampleNextunderstanding-es6

Last updated 4 years ago

  • defineProperty

这个是定义一个原型。

let p = {}
Object.definePrototype(p, 'name', {
    value: function(){console.log('i am name')}
})

这个是定义一个name属性。

注意:defineProperty定义的属性,属性特性(writable, configurable, enumerable)是false, 而字面量创建的对象,默认是true.

  • defineProperties

这个是在原型上定义多个属性

let person = {}
Object.defineProperties(person, {
    names: {
        value: 'name'
    },
    age: {
        value: 'age'
    }
})

defineProperties和defineProperty定义的数据属性,若不指定,默认的内部属性为false

  • {} 这种是属于字面量创建了,默认的内部属性都是true.

    • 数据属性(Configurable, Enumerable, Writeable)默认都是true, Value是undefined.

    • 访问器属性(Configurable, Enumerable, Get, Set)中前两个默认为true, 后两个默认为undefined

  • Object.getPrototypeOf(obj)

这个是获取一个原型

  • Object.setPrototypeOf(original, newer)

这个是设置新的原型,在es6中出.

  • obj.hasOwnProperty 这里是可以判断是否在实例上存在对应的属性。

    let P = function(){}
    P.prototype.name = 'xhs'
    let p = new P
    p.hasOwnProperty("name") // false
    P.hasOwnProperty('name') // true

    只是在对应的目标对象上发生作用。

  • constructor

这个东西比较容易搞混淆。constuctor并不是说函数是一个constructor,而是说被new关键字调用的函数才是构造函数。

所以可以看出一些区别来。

function Foo() {this.name = 'foo-name'}
function Bar() {this.name = 'bar-name'}
Foo.prototype = Bar.prototype //1
Foo.prototype = new Bar //2
Foo.prototype = Object.create(Bar.prototype) //3
  • //1的情况 他是直接继承自Bar的原型的对象,Foo原型上有任何更改,都会作用于Bar的原型.

    const f = new Foo
    b = new Bar
    Foo.prototype.friends = ['john', 'issac']
    console.log(Bar.prototype.friends) //['john', 'issac']
    console.log(b.friends) // ['john', 'issac']
  • //2的情况 他是作为一个构造函数去继承,这样会产生副作用,他会继承Bar的构造函数。

    function Person() {
    this.name = 'person name'
    this.age = 18
    this.friends = []
    }
    
    function Child() {
    this.name = 'child name'
    }
    
    Child.prototype = new Person // new Person is: {name: "person name", age: 18, friends: Array(0)}
    Child.prototype //Person {name: "person name", age: 18, friends: Array(0)}
    let c = new Child
    c.age // 18
    c.friends.push('c')
    let c2 = new Child
    c2.friends // ['c'],这样就会出现副作用(因为把Person的构造函数对象放在了Child的原型上,导致在原型上直接操作)
  • //3的情况 这个就只是单纯的继承,不会出现副作用。

    function Person() {
    this.name = 'person name'
    this.age = 18
    }
    
    function Child() {
    this.name = 'child name'
    }
    
    Person.prototype.friends = []
    
    Child.prototype = Object.create( Person.prototype ) // Object.create(Person.prototype)不会创建实例方法,只会继承prototype方法,但是如果直接操作也会有问题
    
    Child.prototype //Person {}
    let c = new Child
    c.age // undefined, 这里说明不会继承构造函数对象(因为压根就没有使用构造函数~.~)
    c.friends.push('c')
    (new Child).friends // ['c'], 可以发现,这也也是有问题的。这点不难理解吧,因为Object.create( Person.prototype )把Person的原型方法给了Child的prototye,导致直接操作原型的本身了。

    //3 和 //2的区别就是 //2可以获得原型及实例的方法,但是//3只会获得原型链上的方法,不会获得实例上的方法。

这几种不能单纯的说好或者不好,毕竟每种都有他应用的场景。

针对于上面这点,。

具体可以参考

Property Descriptors
constructor介绍
我们来继承
//3在es6中的写法
(Prototypal) Inheritance