javascript USES functions to implement methods of objects
- 2020-06-12 08:29:51
- OfStack
This article illustrates how javascript USES functions to implement objects. Share to everybody for everybody reference. Specific implementation methods are as follows:
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title> Implement objects using functions ( important )</title>
<script type="text/javascript">
function Person(name, age) {
// Custom properties , Simultaneous initialization
this.name = name;
this.age = age;
// Define methods
this.ShowInfo = function () {
alert("name:" + this.name + "--age:" + this.age);
}
}
var ZhangShan = new Person(" zhang 3", 30);
alert("Name:" + ZhangShan["name"] + ",Age:" + ZhangShan.age);
// Note that the first 1 Attribute names are enclosed in brackets and can be passed dynamically
ZhangShan.ShowInfo();
</script>
</head>
<body>
</body>
</html>
Hopefully, this article has been helpful in your javascript programming.