0

I'm posting due to a lack of understanding of a couple of concepts and also to check if my description of this code is accurate.

First I have created parent object called contactForm. I've made this object equal to an object literal which uses literal notation that is, creating a new object with { } and defining properties within the brackets.

Then I have the init method. If you’re familiar with object orientated programming that would be the same things as your constructor method.

Now the next part is where is where I am confused. I am using jQuery to create a new element which are the button tags. Is this newly created element an object inside of the parent object called contactForm?

My second question is am I passing a parameter that sets the text to 'Contact Me!' to the contactForm object or the button element/object?

My final question - is it the case that the parameter passed to the object can also be called a property of that object?

Sorry if I haven't been descriptive enough or accurate enough with my terminology. Any succinct and clearly explained answers would be massively appreciated.

var contactForm = {
        init: function() {

            $('<button></button>', {
                text: 'Contact Me!'
            })
                .insertAfter('article:first');
        }
    }; 

1 Answer 1

2

First I have created parent object called contactForm. I've made this object equal to an object literal which uses literal notation that is, creating a new object with { } and defining properties within the brackets.

You have an object, which is described in your code by an object literal, and assign that to the variable contactForm. Not sure why you would call it "parent".

Then I have the init method. If you’re familiar with object orientated programming that would be the same things as your constructor method.

Your object has a property named "init", which is a function - conveniently this is called "a method". However, constructors in JavaScript work a bit different - every function can construct objects when it is called with the new operator. Of course, a dedicated method to initialize is often used when singleton instances are declared (as literals).

Now the next part is where is where I am confused. I am using jQuery to create a new element which are the button tags. Is this newly created element an object inside of the parent object called contactForm?

No. What you are doing in the init function has (at first) nothing to do with the contactForm object. You are calling the jQuery function, and it returns an object on which you call the insertAfter method, and as you don't do anything with the result of that function it is garbage-collected.

My second question is am I passing a parameter that sets the text to 'Contact Me!' to the contactForm object or the button element/object?

You are passing two parameters: a string and an object. What the jQuery function does with these arguments is unknown to your code. According to the documentation (and to the code, if you really wanted to dig in), this will create a button element (wrapped in a jQuery instance) and call the text method on it.

My final question - is it the case that the parameter passed to the object can also be called a property of that object?

No. You pass parameters into a function. It might happen that the function sets them as properties of a returned object, but that depends on the function. Anyway they are not called "properties of the function object".

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you for investing your time to help me learn and further my knowledge.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.