Skip to content

Using the Constructor Function

A function can be used to configure an object, or even to define a class. The name of the class created in this way is the name of the function.

example:

function person(nama, surname) {
            this.name = name;
            this.surname = surname;
            this.writeID = function () {
                document.write(this.name+" "+this.surname);
            }
        }
        var programmer = new person("furkan", "gülşen");
        programmer.writeID()

output : furkan gülşen

This expression in the example refers to the instance of the class (the object being created). This.name expression also refers to the name property of the generated object.

this.writeID bir fonksiyon ifadesi olarak tanımlanmıştır. Bu da person sınıfının bir metodudur.

In order to create an instance (object) of a class defined in this way, the class operator and its parameters, together with the new operator, are notified. The returned value is an object, and we assign the object to this variable (reference) by assigning it to a variable, as in our example (the programmer is an object).

As seen in the last line, the identity class of the person class is called, and as a result the values of the name and surname of the object are written to the output.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: