Хабр Курсы для всех
РЕКЛАМА
Практикум, Хекслет, SkyPro, авторские курсы — собрали всех и попросили скидки. Осталось выбрать!
function dup(o,i,r) {
r=o;
if(r && typeof o == "object") {
r = o instanceof Array ? [] : {};
for(i in o)
if(o.hasOwnProperty(i))
r[i] = dup(o[i]);
}
return r
}
// 136
function dup(o,i,r){r=o;if(r&&typeof o=="object"){r=o instanceof Array?[]:{};for(i in o)if(o.hasOwnProperty(i))r[i]=dup(o[i])}return r}
// отказ от ie8, 134 символа:
function dup(o,i,r){r=o;if(r&&typeof o=="object"){r=Array.isArray(o)?[]:{};for(i in o)if(o.hasOwnProperty(i))r[i]=dup(o[i])}return r}
function c(o,i,r){r=o;if(r&&typeof o=="object"){r=Array.isArray(o)?[]:{};for(i in o)if(o.hasOwnProperty(i))r[i]=o[i]===o?r:c(o[i])}return r}
function c(o,i,r){if(o&&typeof o=="object"){r=o instanceof Array?[]:{};for(i in o)o.hasOwnProperty(i)?r[i]=o[i]===o?r:c(o[i]):0}return r||o}
o[i] === o ? — как элемент объекта/массива может быть равен самому объекту/массиву?function c(o,p,y){if(o&&typeof o=="object"){p=o instanceof Array?[]:{};for(y in o)o.hasOwnProperty(y)?p[y]=c(o[!p||y]):0}return p||o}
Может быть равенПример? У меня не получилось такой найти, везде false.
function c(o,p,y){
if(o&&typeof o=="object"){
p=o instanceof Array?[]:{};
for(y in o){
if(o.hasOwnProperty(y)){
if(o[y] === o){
p[y] = p
console.log(y + ' true')
} else {
p[y] = c(o[y])
console.log(y + ' false')
}
}
}
}
return p||o
}
c({a:1,b:2,c:{c1:1,c2:{c3:"asdasd", c4: 2*2}}}) // false
c([1,2,3,[4,5,6,[7,8,9]]]) // false
c([1,1,[1,1,[1,1]]]) // false
function dup(object, copy) {
if(typeof object == 'object') {
copy = object instanceof Array ? [] : object && {};
Object.keys(object).forEach(function(key) {
copy[key] = dup(object[key]);
});
}
return copy || object;
}
var object = {
a: {
b: {
c: 1
}
}
}
dup(object);
function dup(o,c){if(typeof o=='object'){c=o instanceof Array?[]:o&&{};Object.keys(o).map(function(k){c[k]=dup(o[k])})}return c||o}
var copy = Object.create(object);
(function($) {
'use strict';
if(!$.map) {
$.map = function(fn, object) {
var i = -1, length = this.length, array = [];
while(i++ < length) {
if(i in this)
array.push(fn.call(object, this[i], i, this));
}
return array;
};
}
})(Array.prototype);
> var a = true, b = false
> if (b=a&&typeof a=="boolean") 1
> 1
> b
> true
> a
> true
o.hasOwnProperty(i), если мы из него же и извлекаем эту i?
DeepClone на javascript, который можно твитнуть