const defaults = {
title: 'wololo',
status: 1
};
const keys = Object.getOwnPropertyNames(defaults);
const validators = {
status(value) {
return !isNaN(value) && isFinite(value);
}
};
const transforms = {
status(value) {
return parseInt(value);
}
};
class MyClass {
constructor(props) {
for(let key of keys) {
if(!Object.prototype.hasOwnProperty.call(props, key)) {
this[key] = defaults[key];
continue;
}
if(typeof validators[key] === 'function' && !validators[key](props[key])) {
this[key] = defaults[key];
continue;
}
this[key] = typeof transforms[key] === 'function' ? transforms[key](props[key]) : props[key];
}
}
}
;((scope, methods) => {
for (let key of Object.getOwnPropertyNames(methods)) {
scope[key] = methods[key];
}
})($scope, {
'method1' : {},
'method2' : {},
//..
'methodn' : {}
});
function eqObj(obj1, obj2) {
if(obj1 === obj2) return true;
if(!obj1 || !obj2) return false;
var keys1 = Object.getOwnPropertyNames(obj1);
var keys2 = Object.getOwnPropertyNames(obj2);
var len = keys1.length;
if(len !== keys2.length) return false;
while(len--) {
var key = keys1[len];
if(!Object.prototype.hasOwnProperty.call(obj2, key)) return false;
if(typeof obj1[key] === 'object' && typeof obj2[key] === 'object' && !eqObj(obj1[key], obj2[key])) return false;
if(obj1[key] !== obj2[key]) return false;
}
return true;
}
var objects = [
{it1: "Pro1", desc: "desc1", id: '17'},
{it1: "Pro2", desc: "desc2", id: '85'},
{it1: "Pro3", desc: "desc3", id: '87'},
{it1: "Pro4", desc: "desc4", id: '41'},
];
var result = objects.filter(function(obj) {
return !eqObj(obj, {it1: "Pro4", desc: "desc4", id: '41'});
});
function Vector(x, y) {
this.x = x;
this.y = y;
}
Vector.prototype.plus = function plus(otherVector) {
var x = this.x + otherVector.x;
var y = this.y + otherVector.y;
return new Vector(x, y);
};
Vector.prototype.minus = function minus(otherVector) {
var x = this.x - otherVector.x;
var y = this.y - otherVector.y;
return new Vector(x, y);
};
Vector.prototype.toString = function toString() {
return 'Vector { x: ' + this.x + ', y: ' + this.y + ' }';
}
console.log(new Vector(1, 2).plus(new Vector(2, 3)));