SoFunction
Updated on 2025-04-10

JavaScript prototype-based objects (create, call)


//A powerful feature of JScript is the ability to define constructors to create custom prototype-based objects for use in your scripts.
//To create an instance of a prototype-based object, you must first define a constructor.
//This process will create a new object and initialize it (create the property and assign the initial value).
//When it is finished, the constructor will return a reference to the constructed object.
//Inside the constructor, the created object is referenced through this statement.
function people(name,age)//Define people object
{
=name;//The mName here represents the attribute, and there is no need to define it outside. This indicates the object of people
=age;
="Mammal";
=Exporting;//Method, note that you can only write toString here, not toString()
=function()//equivalent to =method; then write the method below
{
return "Hello";
}
}
function Exporting()//There can be a return value, but you don't need to write the type of return value before the function name, such as string, int
{
return "My name is—"++", age is—"+;
}
/*function method()
{
return "Hello";
}*/
=function()//Write method outside the constructor,
//You can also write function ()
//Equivalent to the method in the constructor:
{
return ;
}
=;//Write attributes outside the constructor,
//Equivalent to the method in the constructor:
function ()//Same = function() is equivalent
//It is also equivalent to writing in the constructor:
{
return "1000";
}
function show()//Calling people object
{
var me=new people("Andy Lau",22);//Instantiate the people object, keyword new
//var myName=();
//alert(myName);
="Male";//The sex attribute here can only be used for the me instance, that is, the unique attribute
//If there is a definition var you =new people("Xiaoqiang",1);
//You cannot call the sex attribute in this instance
//If you want both instances to be referenced, you should write the sex attribute as
//alert();
//alert();
//alert(());//or just write alert(me)
//alert(());
//alert(());
alert(()+"\nName: "+()+"\nGender: "++"\nCategory: "++"\nTotal assets: "+()+"\nSummary: "+());
}