|
<HTML>
<HEAD>
<TITLE> Object oriented Basics</TITLE>
</HEAD>
<BODY>
 <script>
// A simple function which takes a name and saves it to the current context
function User(name)
 {
this.name = name;
}
//Create a new instance of that function ,with the specified name
var me = new User("zdw");
//We can see that its name has been set as a property of itself
alert(me.name);
//And that it is an instance of the User object
alert(me.constructor == User);
//Now,since User() is just a function,what happens
//when we treat it as such?
User("admin");
//Since its 'this' context wasn't set,it defaults to the global 'window'
//object,meaning that window.name is equal to the name provided
alert(window.name);
//Create a new,simple,Person object
function Person(name)
 {
this.name = name;
}
//Create a new Person object
var p1 = new Person("person");
//Alsos create a new Person Object(from the constructor reference of the first)
var p2 = new p1.constructor("zhangsan");
//show result
alert(p2.name);
</script>
</BODY>
</HTML>

|