MCPcopy Create free account
hub / github.com/Xu22Web/tech-study-js / constructor

Method constructor

tech-study.js:942–973  ·  view source on GitHub ↗
(val, shallow = false)

Source from the content-addressed store, hash-verified

940 _value;
941 value;
942 constructor(val, shallow = false) {
943 const _this = this;
944 this._isShallow = shallow;
945 if (val && typeof val === 'object' && shallow) {
946 const reactiveVal = reactive(val);
947 this._value = reactiveVal;
948 this.value = reactiveVal;
949 }
950 else {
951 this._value = val;
952 this.value = val;
953 }
954 // 定义属性
955 Object.defineProperty(this, 'value', {
956 get() {
957 // 收集依赖
958 trackRef(this);
959 return _this._value;
960 },
961 set(newVal) {
962 // 旧数据
963 const oldVal = this._value;
964 // 数据变化
965 if (oldVal !== newVal) {
966 // 设置新数据值
967 _this._value = newVal;
968 // 通知依赖
969 triggerRef(this, newVal, oldVal);
970 }
971 },
972 });
973 }
974 toJSON() {
975 return this._value;
976 }

Callers

nothing calls this directly

Calls 1

reactiveFunction · 0.70

Tested by

no test coverage detected