JSオブジェクトの作成と継承
以下はJSオブジェクトの一般的なオブジェクトの作成方法です.
<script type="text/javascript">
// javascript
/*1:
//
var obj = new Object();// object
obj.name = "zhang san";
obj["age"] = 20; // obj age
alert(obj.name);
alert(obj.age);
delete obj.name; //
alert(obj.name);
alert(obj.age);
obj.sayName = function(name){ // obj
this.name = name;
alert(this.name);
}
obj.sayName("lisi");
*/
/*
2:
var obj = {username:"zhangsan",password:"12345"};
alert(obj.username);
*/
/*
3:
function createObject(){
var object = new Object();
object["name"] = "zhangshan";
object.password = "1234";
object.get = function(){
alert(this.name +"," + this.password);
}
return object;
}
var object1 = createObject();//
object1.get();
*/
/*
function createObject(username, password){
var object = new Objcet();
object.username = username;
object.password = password;
object.get = function(){
alser(this.username +"," + this.password);
}
}*/
/*
function get(username, password){
alert(this.username +","+ this.password);
}
function createObject(username, password){
var object = new Object();
object.username = username;
object.password = password;
object.get = get
return object;
}
var obj = createObject(" ","1234");
obj.get();*/
/*
4: prototype ( )
prototype object
:1: prototype ,
。
2: prototype
function Person(){
}
Person.prototype.name = "zhangsan";
Person.prototype.age = 13;
Person.prototype.get = function (){
alert(this.name +"," + this.age);
}
var person = new Person();
person.get();
person.name = "lisi";
person.get();*/
/*
5: prototype ,
function Person(){
this.name = "zhangsan";
this.age = 12;
if( typeof Person.flage == "undefined"){
Person.prototype.getInf = function(){
alert(this.name +"," + this.age);
}
this.flage = true;
}
}
var p1 = new Person(); p1.getInf();
var p2 = new Person(); p2.getInf();
*/
</script>
以下は常用対象の継承方式です. <script type="text/javascript">
/*1:
function Parent(username){
this.username = username;
this.sayname = function (){
alert(this.username);
}
}
function Child(username, password){
this.temp = Parent; // this.temp
this.temp(username); // , this Child Child
delete this.temp; // this
this.password = password;
this.sayPass = function (){
alert(this.password);
}
}
var chirld = new Child(" ","123");
chirld.sayname();
chirld.sayPass();*/
/*2: call call function , call(param1, param2,...);
call this
function Parent(username){
this.username = username;
this.sayname = function (){
alert(this.username);
}
}
function Child(username, password){
Perent.call(this, username);
this.password = password;
this.sayPass(){
}
}
*/
/*3: apply
apply call apply apply(Param1, new Array());
function Parent(username){
this.username = username;
this.sayname = function (){
alert(this.username);
}
}
function Child(username, password){
Parent.apply(this, [username]); // Parent.apply(this, new Array(username));
this.password = password;
this.sayPass(){
}
}
*/