-1

WHy is clone not a function in JS? How do I clone?

const standardhours = {
    "09" : '9AM',
    "10" : '10AM',
    "11" : '11AM',
    "12" : 'Noon',
    "13" : '1PM',
    "14" : '2PM',
    "15" : '3PM',
    "16" : '4PM',
    "17" : '5PM',
    "18" : '6PM',
    "19" : '7PM'
};

var availablehours = {
    "09" : '9AM',
    "10" : '10AM',
    "11" : '11AM',
    "12" : 'Noon',
    "13" : '1PM',
    "14" : '2PM',
    "15" : '3PM',
    "16" : '4PM',
    "17" : '5PM',
    "18" : '6PM',
    "19" : '7PM'
};


availablehours = clone(standardhours);
jdog
  • 10,351
  • 29
  • 90
  • 165
  • 8
    Clone is not a function. – vol7ron Sep 10 '18 at 21:34
  • https://stackoverflow.com/q/728360/438992 noting that there are multiple implementations in external libraries, e.g., Lodash, etc. – Dave Newton Sep 10 '18 at 21:35
  • Because `clone()` is not a method belonging to the [`window` object](https://developer.mozilla.org/en-US/docs/Web/API/Window#Methods). You can call methods like `alert()` in that manner because they're in the global context. There is no such method with the handle of `clone`. – Joseph Marikle Sep 10 '18 at 21:37
  • And, what are you actually trying to accomplish because the two object you are showing are already identical as far as I can tell. – Scott Marcus Sep 10 '18 at 21:42

1 Answers1

2

Because .clone() isn't defined anywhere in your code and is not a native part of the JavaScript language or any part of the Global object provided by the host environment.

JQuery defines .clone() as method of a JQuery object, not a function than you can just call.

Also, .cloneNode() is a DOM element API.

Are you looking for Object.assign()?

const standardhours = {
    "09" : '9AM',
    "10" : '10AM',
    "11" : '11AM',
    "12" : 'Noon',
    "13" : '1PM',
    "14" : '2PM',
    "15" : '3PM',
    "16" : '4PM',
    "17" : '5PM',
    "18" : '6PM',
    "19" : '7PM'
};

var availablehours = Object.assign(standardhours);
console.log(availablehours);
Scott Marcus
  • 64,069
  • 6
  • 49
  • 71
  • I am trying to clone the standardhours associative array to available hours. Yes they are equal at the start, but the user might remove some hours say 9AM and 6PM from availablehours, but later I am trying to set the availablehours back to the full list. Thing cloning standardhours as easiest way. – jdog Sep 11 '18 at 03:06