How javascript uses functions to implement objects
This example describes how to use functions to implement objects in javascript. Share it with you for your reference. The specific implementation method is as follows:
?
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
<Html xmlns = "http://www.w3.org/1999/xhtml"> <Head> <Meta http-equiv = "Content-Type" content = "text/html; charset = UTF-8"/> <Title> using functions to implement objects (important) </title> <Script type = "text/javascript"> Function Person (name, age ){ // Define attributes and initialize them at the same time This. name = name; This. age = age; // Define the Method This. ShowInfo = function (){ Alert ("name:" + this. name + "-- age:" + this. age ); } } Var ZhangShan = new Person ("James", 30 ); Alert ("Name:" + ZhangShan ["name"] + ", Age:" + ZhangShan. age ); // Note that the first attribute is enclosed in brackets and the attribute name can be dynamically transferred. ZhangShan. ShowInfo (); </Script> </Head> <Body> </Body> </Html> |
I hope this article will help you design javascript programs.