Article Introduction: The easiest way to create a custom object is to create an instance of object and then add properties and methods to it. |
The easiest way to create a custom object is to create an instance of object and then add properties and methods to it, as follows:
var person = new Object ();
Person.name = "Nicholas";
Person.age = "Person.job"
= "Software Engineer";
Person.sayname = function () {
alert (this.name);
};
Person.sayname ();
The example above creates an object named person and adds three attributes (name, age, and job) and a method (Sayname ()) to it. where the Sayname () method is used to display the value of THIS.name (). Early JavaScript developers often use this pattern to create new objects. But there is an obvious drawback to this approach: creating many objects using the same interface creates a lot of repetitive code. To solve this problem, people began to use a variant of the factory pattern.