Skip to content
Advertisements

JavaScript Object

Although the Javascript world is a language developed for client-side applications, it provides users with an environment in which objects can be defined and used. Some of the objects defined in Javscript can be listed as follows. String, Date, Math, JSON etc. We can define and use objects by ourselves.

Javascript Object Creation

Javascript provides an object named Object as standard. The created objects are also of Object type. For example, we can make the creation of an object as follows:

 varbankAccount = newObject()

In this way, we have created a bankAccount object that does not have any members and only contains members of the bank Object an type. We can define this object in a simpler way as follows.

var bankAccount = {};

So it is possible to define without using the keyword keyword.

Adding Property to Objects

The process of adding properties to the objects we create can be done as follows.

var bankAccount = {};
bankAccount.number = 114521;

we can specify a value or function as property. If a property points to a function, we can call it a method.

var bankAccount = {};
     bankAccount.number = 114521;
     bankAccount.owner = "barry quenn";
     bankAccount.balance = 500;
     bankAccount.details = function () {
       return "Account: " + this.number +
              " belongs to " + this.owner +
              "\nBalance: " + this.balance;
};

The bankAccount object described above is a property named nes details Yukarıda. This keyword used in the methods refers to the object to which the method belongs. For this reason, the above inden details içeris method can be accessed by the other members via this keyword. If we try to access a member without using this keyword from the method, we encounter an error.

Adding Object Members During Creation

It is also possible to identify the members when creating the object as it is possible to identify the members after creating the objects. It can be considered as a display that can be used to clean the code.

var bankAccount = {
    number: 114521,
    owner: "barry quenn",
    balance: 500,
    details: function () {
             return "Account: " + this.number +
                    " belongs to " + this.owner +
                    "\nBalance: " + this.balance;
    }
};

Using Constructor

Create the object described above, let us remember the shape again.In its simplest form the creation of an object.

var bankAccount = {};

What we need to do what we want to add a constructor to this object is the following:

var bankAccount = function (number, owner, balance){};

This function is “{}” immediately before the brackets were determined.Indicates the constructor functions in JavaScript environmentthis they throw to the object named.The environment constructor functions in JavaScript, C#, or Java are similar to classes in.Constructor to create a new object and use that object by using

var BankAccount = function (number, owner, balance){
    this.number = number,
    this.owner = owner,
    this.balance = balance,
    this.details = function() {
      return "Account: " + this.number +
             " belongs to " + this.owner +
             "\nBalance: " + this.balance;
    };
 };

We can use this object as follows.

var myAccount = new BankAccount(914521, "barry quenn", 500);

After you define the constructor function, you can use the siniz new ü operator to create the object instance.

Using Prototypes of Objects

Javascript A special method called prototype is found in all objects. You can add new property or methods to objects by using this method. We can create our own custom types with constructor functions. Although this may seem reasonable for data-retention properties, it can become a problem for methods. We can separate our methods from the type in which we create them.

Example-1: Without Prototype:

var Rectagle = function (x,y) {
     this.x = x;
     this.y = y;
     this.Area = function() { return x*y; };
};

Example-2: With Prototype:

var Rectangle = function (x, y) {
    this.x = x;
    this.y = y;
};
 
Rectangle.prototype = {
    Area: function() { return (this.x * this.y); }
};

Only modify your own prototypes. Never modify the prototypes of standard JavaScript objects.

In Example-2, Area method 2, which is the area computational method, is defined in the prototype method of the type.

What did that benefit us? In Example-1, the function defined in the constructor will create and retain an anonymous function in memory when each Rectangle object is created.

In addition, Rectangle type value holder (x, y) members and functions were separated. It is even possible to distribute this distinction to different Javascript files.

The use of the prototype is primarily useful in increasing the readability of the code, while providing increased performance.

JAVASCRIPT OBJECT: EXAMPLES (CLICK)


Advertisements

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: