Everyone to express their views or ha, I continue to study the well, me too just learn JS Ha, the original only as a simple API



RIA is currently very hot, a lot of java developers also have to start using ajax framework, such as noisy javaeye living Ext!



However, Ext everyone not to forget the foundation of what is JS!



U.S. are the first to use js function, consider the object!



Java introduction to the perspective of today's entry-js one object!



Example: careful study ha!!



Code, see attachment, you can run directly Animal.htm on ok to open IE



The following documents Animal.js!



/ / Define category Animal
/ * function (name, age) is equivalent to java constructor * /
var Animal = function (name, age) (
/ / name, age equivalent of java familiar this.name = name == undefined? 'animal': name;
this.age = age == undefined? 0: age;
);
/ / Definition of function, similar to java methods, java must first define the category, only Ways js-defined functions can be directly
function getName () (
alert ( "hello name is:" + this.name);
)
;

function setName (name) (
this.name = name;
)
;
/ / Through the prototype to the category of Animal increased getName function pointer point to function getName
Animal.prototype.getName = getName;
Animal.prototype.setName = setName;
/ / Equivalent to anonymous function
Animal.prototype.sayAge = function () (
alert ( "hello age is:" + this.age);
);
/ / Similar to the java code
/ *
public class Animal (
private String name;
private int age;

public Animal (String name, int age) (
this.name = name;
this.age = age;
)

public String getName () (
return name;
)

public void setName (String name) (
this.name = name;
)

public void sayAge () (
this.age = age;
)
)
* * /



The following documents Animal.htm!

<! DOCTYPE HTML PUBLIC "- / / W3C / / DTD HTML 4.01 Transitional / / EN"
" http://www.w3.org/TR/html4/loose.dtd ">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=GBK">
<script type="text/javascript" src="Animal.js"> </ script>
<title> </ title>
</ head>
<body onload="doit()">

</ body>

<script type="text/javascript">

function doit () (
/ * Target animal, animal1 attributes like, huh, huh what do think about java * /
var animal = new Animal ();
animal.getName ();//
animal.sayAge ();

var animal1 = new Animal ( 'kite', 12);
animal1.getName ();//
animal1.sayAge ();

/ / Change the object of animal property animal.setName ( 'tomcat');
alert ( "the implementation of animal.setName ( 'tomcat'); after");
animal.getName ();//
animal1.getName ();//

)

</ script>

</ html>