Home >Web Front-end >JS Tutorial >Defining object categories in Javascript_javascript tips

Defining object categories in Javascript_javascript tips

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

From: JavaEye.com

Pay attention to the definition of object categories in JavaScript, use function to define object categories, and initialize objects using the new operator
function Person(name, age) {
this.name = name;
this.age = age;
this.toString = function() {
document.writeln("[name]:" this.name "
" "[age]:" this .age);
}
}

Complete simple HTML



JavaScript






<script> <BR>function Person(name, age) { <BR> this.name = name; <BR> this.age = age; <BR> this.toString = function() { <BR> document.writeln("[name]:"+this.name+"<br>"+"[age]:"+this.age); <BR> } <BR>} <BR></script><script> <BR> var person = new Person(); <BR> person.name="robbin"; <BR> person.age=30; <BR> person.toString(); <BR></script>
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