Метод Object.assign
является частью стандарта ECMAScript 2015 (ES6) и делает именно то, что вам нужно.
var clone = Object.assign({}, obj);
Метод Object.assign () используется для копирования значений всех перечислимых собственных свойств из одного или нескольких исходных объектов в целевой объект.
Полипол для поддержки старых браузеров:
if (!Object.assign) { Object.defineProperty(Object, 'assign', { enumerable: false, configurable: true, writable: true, value: function(target) { 'use strict'; if (target === undefined || target === null) { throw new TypeError('Cannot convert first argument to object'); } var to = Object(target); for (var i = 1; i < arguments.length; i++) { var nextSource = arguments[i]; if (nextSource === undefined || nextSource === null) { continue; } nextSource = Object(nextSource); var keysArray = Object.keys(nextSource); for (var nextIndex = 0, len = keysArray.length; nextIndex < len; nextIndex++) { var nextKey = keysArray[nextIndex]; var desc = Object.getOwnPropertyDescriptor(nextSource, nextKey); if (desc !== undefined && desc.enumerable) { to[nextKey] = nextSource[nextKey]; } } } return to; } }); }
После сценария Perl я использую для маркировки первых несогласованных круглых скобок:
#!/usr/bin/perl
use strict;
undef $/;
$_= <>; # slurp input
my $P = qr{[^()]*}; # $P = not parentheses
# repace matched parentheses by marks
while(s! \( ($P) \) !\cA$1\cB!gx){}
while(s!^ ($P) \( (.*) \) ($P) $ !$1\cB$2\cB$3!sgx){}
s!([()])! → $1!; # mark the first unmatched ()
y!\cA\cB!()!; # replace marks
print
Использование:
$ cat f
1(2(3(4(5)6)7)8(9)10(
11(12)13)14) (15 ( and )
(16(17(18)19)
20)21(22)23
$ parentesis f
1(2(3(4(5)6)7)8(9)10(
11(12)13)14) → (15 ( and )
(16(17(18)19)
20)21(22)23