我的解決方法是:參考jquery中extend方法的實現(xiàn),通過isPlainObject 函數(shù)判斷該對象是否具有通過構造函數(shù)繼承的原型上的屬性。請問,我這個解決方法有什么問題嗎? isPlainObject = function(obj) { if (obj.constructor && !hasOwn.call(obj, "constructor") && !hasOwn.call(obj.constructor.prototype, "isPrototypeOf")) { return false; } var key; for (key in obj) { } return key === undefined || hasOwn.call(obj, key);}, //測試,對于對象x不進行深拷貝function O() { this.yyy = 'yyy';}function X() { this.xxx = 'xxx';}X.prototype = new O();x = new X();obj1 = { a : 'a', b : 'b', y : '1' };obj2 = { x : { xxx : 'xxx', yyy : 'yyy' }, y : 'y' };var combineObj = $.extend(true,obj1,obj2);console.log(combineObj);
迅雷面試題:深拷貝對象,除了原型上的屬性?
慕工程0101907
2018-07-31 16:19:06