JS対象者向け
3572 ワード
1.オブジェクトの作成
window.onload=function(){
//1. :
function student1(name,qq){
var obj=new Object();
obj.name=name;
obj.qq=qq;
obj.show=function(){
alert(this.name+":"+this.qq);
}
return obj;
}
var s1=student1("HH","123");
console.log(s1);
//2. :
function Student2(name,qq){
this.name=name;
this.QQ=qq;
this.show=function(){
alert(this.name+":"+this.QQ);
}
}
var s2=new Student2("HH","1234");
console.log(s2);
//3. :
var s3={
"name":"HH",
"QQ":"12345",
show:function(){
alert(this.name+":"+this.QQ);
//alert(name+":"+QQ);// !!!
}
}
//s3.show();
//console.log(s3.name+":"+s3.QQ);
console.log(s3);
// , prototype , new prototype 。 :
console.log(s1.prototype);//undefined;
console.log(student1.prototype);//Object;
console.log(s2.prototype);//undefined;
console.log(Student2.prototype);//Object;
console.log(s3.prototype);//undefined;
function Student4(){ };
Student4.prototype = {
name:"fdf",
age:"fd",
// constructor object
//
constructor :Student
};
var s4 = new Student();
alert(s4.constructor);
}
2.原型window.onload=function(){
function Student(){};
Student.prototype.name="HH";
Student.prototype.show=function(){
alert("I am "+this.name);
}
var s=new Student();
//s.show();
//alert(s.name);
Student.prototype.name="FF";
//alert(s.name);
// :1. ; 2.
function Student1(){
this.name="FF";
};
Student1.prototype.name="HH";
Student1.prototype.show=function(){
alert("I am "+this.name);
}
var s1=new Student1();
console.log(s1.name);//FF
console.log(s1.__proto__.name);//HH
console.log(Student1.prototype.name);//HH
Student1.prototype.name="O_O";
console.log(s1.name);//FF
console.log(s1.__proto__.name);//O_O
console.log(Student1.prototype.name);//O_O
//
}
3.ハイブリッドモードwindow.onload=function(){
// : +
function Student(name,QQ){
this.name=name;
this.QQ=QQ;
if(typeof this.show != "function"){
Student.prototype.show=function(){
//alert(this.name+":"+this.QQ);
console.log(this); // this s1
}
}
}
var s1=new Student("MM","1242");
s1.show();
var s2=new Student("QQ","w809r809ew");
console.log(s1.show==s2.show);//true; Student show, Student , 。
}