Home  >  Article  >  Web Front-end  >  JavaScript uses prototype to define object types (conversion) [_javascript skills

JavaScript uses prototype to define object types (conversion) [_javascript skills

WBOY
WBOYOriginal
2016-05-16 19:22:461279browse

From: JavaEye.com

prototype provides a set of JavaScript object-oriented infrastructure. We can use it 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 "
" "[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:



< title>Test Object





Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn