4 Star 1 Fork 3

K. / define-props.js

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
贡献代码
同步代码
取消
提示: 由于 Git 不支持空文件夾,创建文件夹后会生成空的 .keep 文件
Loading...
README
MIT

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';
		}
	}
})
The MIT License (MIT) Copyright (c) 2018 曾建凯 Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

简介

Object.defineProperties 的简化操作函数 展开 收起
MIT
取消

发行版

暂无发行版

贡献者

全部

近期动态

加载更多
不能加载更多了
NodeJS
1
https://gitee.com/janpoem/define-props.js.git
git@gitee.com:janpoem/define-props.js.git
janpoem
define-props.js
define-props.js
master

搜索帮助