Creating JavaScript Objects
With JavaScript, you can define and create your own objects.
There are two different ways to create a new object:
- Define and create an instance of an object (directly created)
person=New Object ();p erson.firstname= "Bill";p erson.lastname= "Gates";p erson.age =56;p erson.eyecolor= "Blue";
Or
Person={firstname: "John", LastName: "Doe", Age:50,eyecolor: "Blue"};
- Use a function to define an object, and then create a new object instance
constructor method to create a class
constructor method to create a class
function person (firstname,lastname,age,eyecolor) {this. firstname=FirstName; this. lastname=LastName; this. age= age; this. eyecolor=Eyecolor;}
Myfather=new person ("Bill", "Gates", "a", "Blue");
<!DOCTYPE HTML><HTML><Head><MetaCharSet= "Utf-8"><title>Beginner's Tutorial (runoob.com)</title></Head><Body><Script>functionEmployee (name,jobtitle,born) { This. Name=name; This. JobTitle=JobTitle; This. Born=born;}varEmployee=NewEmployee ("Fred Flintstone","Caveman",1970);//1. Property extension operations in the prototype object of a classEmployee.prototype.salary=NULL; Employee.salary=20000;d Ocument.write (fred.salary);//2. Method extension operations in the prototype object of a classEmployee.prototype.sayHello= function() {alert ('Hello:' + This. name);} Employee.sayhello (); //= = Eject Hello:tom</Script></Body></HTML>
Reference: https://www.cnblogs.com/polk6/p/4492757.html
Creating JavaScript classes and objects prototype