var obj = { setClassToElement: function(element, styles) { return element.styles = styles; }, createElement: function(type, className) { var element = document.create(); this.setClassToElement(element, styles); } }; 

Closed due to the fact that the issue is too common for the participants Kromster , VenZell , user194374, Denis , Mr. Black 24 Jul '16 at 5:36 .

Please correct the question so that it describes the specific problem with sufficient detail to determine the appropriate answer. Do not ask a few questions at once. See “How to ask a good question?” For clarification. If the question can be reformulated according to the rules set out in the certificate , edit it .

    2 answers 2

    This code creates an obj object with two functions: setClassToElement and createElement .

    return element.styles = styles; assigns the styles property to the object passed to the element parameter and returns this value from the function.

    In addition, it contains an error: the document object does not have a create method, therefore, when called, it will most likely have an error.

    It also does not use the className parameter, but tries to take the styles variable global for this function.


    In order to correct the errors, you need to decide on the fact: what exactly is expected from this code?

    After that, go to help

    Find there that the create element function is called createElement

    Pay attention to the fact that this function accepts the tag parameter of the element being created .

    Therefore, its call should be like this: document.createElement(type);

    Further, note that instead of the className parameter, the unknown variable styles is passed to the called function this.setClassToElement , so you should still replace them

     this.setClassToElement(element, className); 

    Further, the createElement function does not return anything and does nothing with the created element — in fact, it is now useless.

    To make sense, you can return the created item after all the settings.

     return element; 

    To see the created element on the screen - it must be added to the DOM, for this you can use the function appendChild .

    To use - you need to decide on the element where you want to add. For example, you can use the body element.

    in the end, the call will look like this:

     document.body.appendChild(obj.createElement('h1', 'container')); 

    Now you should pay attention to the setClassToElement function, since it accepts the HTMLElement parameter , you need to go to help and see that this interface has no styles property, but only the style property — where you can write the css rules directly. To assign an element to a class, you can use the className property .

     element.className = styles; 
    • What does the line return element.styles = styles ;? - Haski
    • @Haski, do you have any suggestions on this? - Grundy
    • Well, return, it returns the value and then stops the function, but this piece is not clear element.styles = styles - Haski
    • @Grundy, does the element have styles ? It seems style only - Mr. Black
    • @Doofy, etozh javascript, if the object has no property, then when assigned it will create it - Grundy

    A variable obj is declared which contains two methods: setClassToElement and createElement, described in the declaration.

    After this code you can write:

     obj.createElement("button", "my-button"); 

    It can be assumed that using DOM in these methods, a class is set for an element and an element is created accordingly.

    In general, the code specified in the question is rather abstract. He doesn't seem to do any specific things.

    • Is it possible to call an object a variable in this case? - Mr. Black
    • Well, let it be an object. - Ambient Occlusion