Example 1: js defineProperty
var o = {};
Object.defineProperty(o, 'a', {
value: 37,
writable: true,
enumerable: true,
configurable: true
});
var bValue = 38;
Object.defineProperty(o, 'b', {
get() { return bValue; },
set(newValue) { bValue = newValue; },
enumerable: true,
configurable: true
});
o.b;
Object.defineProperty(o, 'conflict', {
value: 0x9f91102,
get() { return 0xdeadbeef; }
});
Example 2: array.splice javascript
const months = ['Jan', 'March', 'April', 'June'];
months.splice(1, 0, 'Feb');
console.log(months);
months.splice(4, 1, 'May');
console.log(months);
months.splice(0, 1);
console.log(months);
Example 3: js defineProperty
var obj = {};
var descriptor = Object.create(null);
descriptor.value = 'static';
Object.defineProperty(obj, 'key', descriptor);
Object.defineProperty(obj, 'key', {
enumerable: false,
configurable: false,
writable: false,
value: 'static'
});
function withValue(value) {
var d = withValue.d || (
withValue.d = {
enumerable: false,
writable: false,
configurable: false,
value: value
}
);
if (d.value !== value) d.value = value;
return d;
}
Object.defineProperty(obj, 'key', withValue('static'));
(Object.freeze || Object)(Object.prototype);
Example 4: javascript padstart
export function padStart(number, digits = 2, emptyDigit = 0) {
let length = 0;
let n = Math.abs(number);
let absoluteNumber = n;
do {
n /= 10;
length++;
} while (n >= 1);
const prefix = Array(Math.max((digits - length) + 1, 0)).join(emptyDigit);
return number < 0 ? `-${prefix}${absoluteNumber}` : prefix + number;
}
export default padStart;