1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261
| const EMPTY_OBJ = (process.env.NODE_ENV !== 'production') ? Object.freeze({}) : {};
const EMPTY_OBJ_1 = Object.freeze({}); EMPTY_OBJ_1.name = 'sun'; console.log(EMPTY_OBJ_1.name);
const EMPTY_OBJ_2 = Object.freeze({ props: { mp: 'hello'}}); EMPTY_OBJ_2.props.name = 'sun'; EMPTY_OBJ_2.props2 = 'props2'; console.log(EMPTY_OBJ_2.props.name); console.log(EMPTY_OBJ_2.props2); console.log(EMPTY_OBJ_2);
console.log('--------------------')
const EMPTY_ARR = (process.env.NODE_ENV !== 'production') ? Object.freeze([]) : [];
EMPTY_ARR.length = 3; console.log(EMPTY_ARR.length)
console.log('--------------------')
NOOP 空函数 使用场景: 1.方便判断 2.方便压缩 const NOOP = () => { }
const instance = { render: NOOP }
const dev = true; if (dev) { instance.render = function () { console.log('render'); } }
if (instance.render === NOOP) { console.log('i') }
console.log('--------------------')
const NO = () => false
console.log('--------------------')
const onRE = /^on[^a-z]/; const isOn = key => onRE.test(key);
console.log(isOn('onChange')); console.log(isOn('onchange'));
console.log('--------------------')
const isModelListener = key => key.startsWith('onUpdate')
isModelListener('onUpdate:change')
console.log('--------------------')
const extend = Object.assign;
const data = { name: 'sun' }; const data2 = extend(data, { age: 18, name: 'moon' }); console.log(data); console.log(data2);
console.log('--------------------')
const remove = (arr, el) => { const i = arr.indexOf(el); if (i > -1) { arr.splice(i, 1); } }
const arr = [1,2,3]; remove(arr, 3); console.log(arr)
console.log('--------------------')
hasOwn 是不是自己本身的属性 const hasOwnProperty = Object.prototype.hasOwnProperty; const hasOwn = (val, key) => hasOwnProperty.call(val, key);
let a = hasOwn({ __proto__: { a: 1 }}, 'a'); let b = hasOwn({ a: undefined }, 'a');
console.log(a, b);
console.log('--------------------') const toTypeString = val => Object.prototype.toString.call(val)
const isArray = Array.isArray
const isMap = val => toTypeString(val) === '[object Map]'
const map = new Map(); const o = { p: 'Hello World' } map.set(o, 'content'); map.get(o); console.log(isMap(map))
const isSet = val => toTypeString(val) === '[object Set]'
const isDate = val => val instanceof Date; console.log(isDate(new Date()), 121) console.log(isDate({ __proto__: new Date() }))
const isFunction = val => typeof val === 'function';
const isString = val => typeof val === 'string';
const isSymbol = val => typeof val === 'symbol';
const isObject = val => val!== null && typeof val === 'object'
const isPromise = val => { return isObject(val) && isFunction(val.then) && isFunction(val.catch); } const p1 = new Promise((resolve, reject) =>{ resolve('sun'); }); console.log(isPromise(p1), 141)
const objectToString = Object.prototype.toString;
const toRawType = val => { return Object.prototype.toString.call(val).slice(8, -1); } console.log(toRawType(''), 150)
const isPlainObject = val => { return toTypeString(val) === '[object Object]' }
const isIntegerKey = key => isString(key) && key !== 'NaN' && key[0] !== '-' && '' + parseInt(key, 10) === key;
console.log(isIntegerKey('a'), 163); console.log(isIntegerKey('1'), 164); 'abc'.charAt(0)
function makeMap(str, expectsLowerCase) { const map = Object.create(null); const list = str.split(','); for (let i = 0; i < list.length; i++) { map[list[i]] = true; } return expectsLowerCase ? val => !!map[val.toLowerCase()] : val => !!map[val]; } const isReservedProp = makeMap(',key,ref,' + 'onVnodeBeforeMount,onVnodeMounted,' + 'onVnodeBeforeUpdate,onVnodeUpdated,' + 'onVnodeBeforeUnmount,onVnodeUnmounted');
console.log(isReservedProp('key'), 182)
const cacheStringFunction = fn => { const cache = Object.create(null); return (str => { const hit = cache[str]; return hit || (cache[str] = fn(str)) }) }
const hasChanged = (val,oldVal) => val !== oldVal && (val === val || oldVal === oldVal)
const invokeArrayFns = (fns, arg) => { for (let i = 0; i < fns.length; i++) { fns[i](arg); } };
const arr = [ function(val) { console.log(val + ' is me'); }, function (val) { console.log("I'm " + val) } ] invokeArrayFns(arr, 'sun')
const def = (obj, key, value) => { Object.defineProperty(obj, key, { configurable: true, enumerable: false, value }); };
const toNumber = val => { const n = parseFloat(val); return isNaN(n) ? val : n; } console.log(toNumber('111'), 237) console.log(toNumber('a111'), 238)
let_globalThis; const getGlobalThis = () => { return (_globalThis || (_globalThis = typeof globalThis !== 'undefined' ? globalThis : typeof self !== 'undefined' ? self : typeof window !== 'undefined' ? window : typeof global !== 'undefined' ? global : {})); };
|