In this task it is not clear why to create var classes = obj.className at this point? obj.className.split (''): []; empty array? Somewhere there was a statement that in order to have an empty string in the property of the object, because the task indicated that the function would not add extra spaces. введите сюда код

The object has a className property that contains a list of “classes” - words separated by a space:

var obj = {className: 'open menu'} Create a function addClass (obj, cls), which adds the class cls to the list, but only if it is not already there:

 addClass(obj, 'new'); // obj.className='open menu new' addClass(obj, 'open'); // без изменений (класс уже существует) addClass(obj, 'me'); // obj.className='open menu new me' alert( obj.className ); // "open menu new me" PS Ваша функция не должна добавлять лишних пробелов. function addClass(obj, cls) { var classes = obj.className ? obj.className.split(' ') : []; for (var i = 0; i < classes.length; i++) { if (classes[i] == cls) return; } classes.push(cls); // добавить obj.className = classes.join(' '); } var obj = { className: 'open menu' }; addClass(obj, 'new'); addClass(obj, 'open'); addClass(obj, 'me'); alert(obj.className) // open menu new me 
  • one
    Then, when an empty className ''.split(' ') creates a non-empty array, which in this case doesn’t need nafig - andreymal
  • Those. when using a space as a separator, when an empty property is created an array with an element in the form of an empty string, I understand correctly? - ZdraviSmisl
  • Almost, for any splitter, a blank line will create a non-empty array with an empty line inside - andreymal

0