📜 ⬆️ ⬇️

Javascript Creating objects

JavaScript provides developers with the ability to create objects and work with them. For this there are the following techniques:



Use the new operator



This is probably the easiest way to create an object. You simply create the name of the object and equate it with the new Javascript object.
')

 // Create our object
 var MyObject = new Object ();
 // Variables
 MyObject.id = 5;  //Number
 MyObject.name = "Sample";  //Line
 // Functions
 MyObject.getName = function ()
 {
     return this.name;
 } 


The disadvantage of this method is that you can work with only one newly created object.


 // Use our object
 alert (MyObject.getName ()); 


Literal notation



Literal notation is a somewhat unusual way of defining new objects, but easy enough to understand. Literal notation works with Javascript 1.3.


 // Create our object using literal notation
 MyObject = {
     id: 1,
     name: "Sample",
     boolval: true
     getName: function ()
     {
         return this.name;
     }
 } 


As you can see, it is quite simple.


 Object = {
 id: value,
 ...
 } 


And an example of use:


 alert (MyObject.getName ()); 


Object constructors



Object constructors are a powerful tool for creating objects that can be used repeatedly. The object constructor is, in fact, a regular Javascript function, to which various parameters can also be passed.


 function MyObject (id, name)
 {

 } 


We just wrote a constructor. With it, we will create our object.


 var MyFirstObjectInstance = new MyObject (5, "Sample");
 var MySecondObjectInstace = new MyObject (12, "Othe Sample"); 


Thus, we created different instances of the object. Now we can work separately with each instance of the MyObject object, without fear that by changing the properties of one instance, we will affect the properties of another instance.


As in OOP, MyObject can have methods and various properties. Properties can be assigned default values ​​or values ​​passed by the user in the object's constructor.


 function MyObject (id, name)
 {
     // User submitted values
     this._id = id;
     this._name = name;
     // Default value
     this.defaultvalue = "MyDefaultValue"; 
 } 


Similarly, we can create functions.


 function MyObject (id, name)
 {
     this._id = id;
     this._name = name;
     this.defaultvalue = "MyDefaultValue"; 
    
     // Get the current value
     this.getDefaultValue = function ()
     {
         return this.defaultvalue;
     }
    
     // Set New Value
     this.setDefaultValue = function (newvalue)
     {
         this.defaultvalue = newvalue;
     }
    
     // Arbitrary function
     this.sum = function (a, b)
     {
         return (a + b);
     }
 } 



Associative arrays



Such a method would be useful in ordering a large number of similar objects.


 var MyObject = new Number ();
 MyObject ["id"] = 5;
 MyObject ["name"] = "SampleName"; 



To bypass such objects, you can use the following loop:


 for (MyElement in MyObject)
 {
     // Bypass Code
     // In MyElement, the record ID
     // In MyObject [MyElement] - the content of the entry
 }



A small scheme has been prepared for the material.





You can view it in the following formats: PNG SVG


Thank you so much gro for your help.

Source: https://habr.com/ru/post/17613/


All Articles