JavaScript uses prototype to define object types_prototype
May 16, 2016 pm 07:19 PMFrom: JavaEye.com prototype provides a set of JavaScript object-oriented infrastructure, which we can use for object-oriented programming. The way to define object types is as follows:
var Person = Class.create(); Person.prototype = { initialize : function(name, age) { this.name = name; this.age = age; }, toString : function() { document.writeln("[name]:" this.name "<br>" "[age ]:" this.age); } }
First use Class.create() to create an object type, and then define the object type. Note that the initialize method is the constructor of Person. The complete HTML is as follows:
<html>
<head>
< meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Test Object</title>
<script type="text/ javascript" src="prototype.js"></script>
</head>
<body>
<script type="text/javascript"></DIV> ;
<DIV class=code>var Person = Class.create();
Person.prototype = {
initialize : function(name, age) {
this.name = name;
this.age = age;
},
toString : function() {
document.writeln("[name]:" this.name "<br>" "[age]:" this.age);
}
}</DIV>
<DIV class=code>var person = new Person("robbin",30);
person.toString();
</script>
</body>
</html>

Hot Article

Hot tools Tags

Hot Article

Hot Article Tags

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

Replace String Characters in JavaScript

Custom Google Search API Setup Tutorial

8 Stunning jQuery Page Layout Plugins
