New Research: Supply Chain Attack on Axios Pulls Malicious Dependency from npm.Details →
Socket
Book a DemoSign in
Socket

define-props

Package Overview
Dependencies
Maintainers
1
Versions
6
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

define-props

a simplified function for Object.defineProperties.

latest
npmnpm
Version
0.0.7
Version published
Weekly downloads
4
-33.33%
Maintainers
1
Weekly downloads
 
Created
Source

define-props.js

Object.defineProperties 的简化操作函数

安装说明

npm install define-props
yarn add define-props

函数说明

defineProps(obj, props, defaults)

obj 目前仅限制为不得为 undefinednull

defaults 则必须为 {} 结构,defaults 的有效字段为:

  • defaults.enumerable 是否可枚举,默认 true
  • defaults.writable 是否可写入,默认 false
  • defaults.configurable 是否可配置(delete key),默认 false

props 可以是 []{}function

prop 表示每一个属性的声明

  • prop.value 属性值
  • prop.get getter方法
  • prop.set setter方法
  • prop.enumerable 参见 defaults
  • prop.writable 参见 defaults
  • prop.configurable 参见 defaults

实际使用发现 reference 有问题,暂时放弃这个声明,下一个版本提供一个 clone 的声明,复制对象值。

const a = defineProps({}, {
	// a.key1 = ‘value1’ 
	// enumerable = true, configurable = false, writable = false
	key1: 'value1',
	// a.key2 = 'value2'
	// 有 value 字段,则取 value 字段的值
	key2: {
		value:      'value2',
		enumerable: true,
		writable:   true
	},
	// a.key3 = {a: 11, b: 22}
	// 无 value 字段,则以整个 {} 作为值
	key3: {
		a: 11,
		b: 22,
	},
	// a.key4 = 'value4'
	// 以一个函数为值,则直接作为 getter
	key4: () => {
		return 'value4'
	},
	// a.key5 = 'value5'
	// 如果 {} 未指定 value 字段,但指定了 get 方法(set不算)
	key5: {
		get: () => {
			return 'value5'
		}
	},
	// a.key6 = undefined
	// 当值为 null or undefined 的时候,判断是否指定了 get 或 writable = true,否则将不会将该键值对写入到对象属性
	key6: null,
	// 同上
	key7: {
		value: null
	},
	// a.key8 = null
	// 由于该键允许写入,所以将 key8 写入到对象属性中
	key8: {
		value:    null,
		writable: true
	},
	// a.key9 = 'value9'
	// 因为存在 get ,所以也会将 key9 写入对象属性中
	key9: {
		value: null,
		get  : () => {
			return 'value9';
		}
	}
})

Keywords

Object.defineProperty

FAQs

Package last updated on 08 Nov 2018

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts