我正在尝试检查是否在我的对象上调用了Object.assign
,如果这是假的,我会对该对象做进一步的工作。我该怎么做呢?我的代码如下所示:
if (Object.assign) {... }
答案 0 :(得分:0)
Object.assign只是将属性复制到对象上。要确定是否正在设置属性,您可以使用Proxy。您无法阻止Object.assign
仅对您的对象起作用。你可以:
Object.assign
,因此它不设置属性(错误的想法)
let validator = {
set: function(obj, prop, value) {
console.log('an attempt to set values has been made', obj, prop, value);
// The default behavior to store the value
obj[prop] = value;
return true;
}
};
let person = new Proxy({}, validator);
Object.assign(person, {a: 2});
我想停止在对象上调用object.assign
这是一个我真的不推荐的黑客
var objectThatCannotBeObjectAssigned = {a:1};
var oldAssign = Object.assign;
Object.assign = function(target) {
if (target === objectThatCannotBeObjectAssigned) {
return target;
}
return oldAssign.apply(this, arguments);
}
console.log(Object.assign(objectThatCannotBeObjectAssigned, {a: 2}) );
console.log(Object.assign({a:1}, {a: 2}));