Jsオブジェクトの例と構造関数の練習
8280 ワード
myObj.js:
/*function f1(){//Code}
var f1=function(){//Code}
var myFunction=new Function('f1','code')*/
//
var myObj=new Object();
//ar myObj={}; // myObj new
myObj.name='sidihu';// myObj
myObj.age='26';
// myObj
myObj.showName=function(){
alert(this.name);
}
myObj.showAge=function(){
alert(this.age);
}
myObj.showSomething=function(){
alert('showSonething');
}
//function
var myFun1=function(message){
alert(message);
this.message=message;
}
var myFun=function(){};
//myFun
// myFun
// prototype myFun myFun y prototype myFun myFun
// prototype prototype myFun myFun showAge() showAge() age
myFun.name='sidihu';
myFun.age='26';
myFun.showName=function(){
alert(this.name);
}
myFun.prototype.showAge=function(){
alert(this.age);
}
myFun.prototype.showSomething=function(){
alert('showSonething');
}
//
var myPrivate=function(message){
this.message=message;
//
//
var separate="-";
var myOwner=this;
//
function alertMessage(){
alert("message is "+separate+myOwner.message);
}
//
//alertMessage();
// ( )
//getPrivate
this.getPrivate=function(){
alert("getPrivate:message is "+separate+myOwner.message);
}
}
//
myPrivate.prototype.showMessage=function(){
alert(this.message);
}
myPrivate.prototype.showSeparate=function(){
alert(this.showSeparate);
}
ページjs:<script language="javascript" src="js/MyObject.js" type="text/javascript" ></script>
<script language="javascript" type="text/javascript" >
window.onload=function(){
//myObj.name='ok'; // name
//myObj.showName();// myObj
// myObj myObj2 myObj2
/*var myObj2= myObj;
myObj2.showName=function(){ alert('i from myobj2 '+this.name);}//myObj2 myObj showName
myObj2.showAge();//26
myObj.age='30';
alert(myObj2.age)// age 30
alert(myObj.age)//30 myObj2 myObj age
myObj2.showName();// i from myobj2 sidihu*/
//var fromMyFun1=new myFun1("i am fun1");//myFun1 myFun1 myFun1 this fromMyFun1, fromMyFun1 meaage
//alert(fromMyFun1.message);
/*// myFun
myFun.showName();
//myFun.showAge();// showAge prototype
var fromFun=new myFun();
fromFun.showAge();// fromMyfun myFun */
var fromPrivate=new myPrivate('message');
fromPrivate.showMessage();//
fromPrivate.getPrivate();
}
</script>