Fork me on GitHub

Javascript面向对象基础

var emptyObject1 = {};             //创建空对象
var emptyObject2 = new Object(); //创建空对象
var person = {"name":"sdcyst",
          "age":18,
          "sex":"male"};     //创建一个包含初始值的对象person
alert(person.name);              //sdcyst
alert(person["age"]);            //18
var person = {};
person.name = "sdcyst";
person["age"] = 18;
alert(person.name + "__" + person.age); //sdcyst__18

var _person = {name:"balala","age":23}; //在构建一个对象时,属性的名字可以不用引号来标注(name),
                    //但是仍旧是一个字符串类型.在访问的时候[]内仍旧需要引号
alert(_person["name"] + "__" + person.age); //balala__23
alert(_person[name]);                   //undefinied
var name = {"name1":"NAME1","name2":"NAME2","name3":"NAME3","name4":"NAME4"};
var namestring = "";
for(var props in name) {  //循环name对象中的属性名字
    namestring += name[props];
}
alert(namestring);  //NAME1NAME2NAME3NAME4

namestring = "";
for(var i=0; i<4; i++) {
    namestring += name["name"+(i+1)];
}
alert(namestring);  //NAME1NAME2NAME3NAME4
var name = {"name1":"NAME1","name2":"NAME2","name3":"NAME3","name4":"NAME4"};
var namestring = "";
for(var props in name) {  //循环name对象中的属性名字
    namestring += name[props];
}
alert(namestring);  //NAME1NAME2NAME3NAME4

delete name.name1;  //删除name1属性
delete name["name3"];  //删除name3属性
namestring = "";
for(var props in name) {  //循环name对象中的属性名字
    namestring += name[props];
}
alert(namestring);  //NAME2NAME4

alert("name1" in name); //false
alert("name4" in name); //true
var date = new Date();
alert(date.constructor);  //Date
alert(date.constructor == "Date");  //false
alert(date.constructor == Date);  //true
var array1 = [];  //创建空数组
var array2 = new Array();  //创建空数组
array1 = [1,"s",[3,4],{"name1":"NAME1"}]; //
alert(array1[2][1]);  //4   访问数组中的数组元素
alert(array1[3].name1); //NAME1 访问数组中的对象
alert(array1[8]);   //undefined
array2 = [,,];  //没有数值填入只有逗号,则对应索引处的元素为undefined
alert(array2.length); //3
alert(array2[1]);     //undefined
var array = new Array(10);
alert(array.length);   //10
alert(array[4]);       //undefined
array[100] = "100th";  //这个操作会改变数组的长度,同时将10-99索引对应的值设为undefined
alert(array.length);   //101
alert(array[87]);      //undefined
var array = new Array("n1","n2","n3","n4","n5");  //五个元素的数组
var astring = "";
for(var i=0; i<array.length; i++) {           //循环数组元素
    astring += array[i];
}
alert(astring);       //n1n2n3n4n5
delete array[3];                   //删除数组元素的值
alert(array.length + "_" + array[3])  //5_undefined

array.length = 3;    //缩减数组的长度
alert(array[3]);     //undefined
array.length = 8;    //扩充数组的长度
alert(array[4]);     //undefined
var array = new Array("no1","no2");
array["po"] = "props1";
alert(array.length);   //2
//对于数组来说,array[0]同array["0"]效果是一样的(?不确定,测试时如此)
alert(array[0] + "_" + array["1"] + "_" + array.po);//no1_no2_props1
function myprint(s1,s2,s3) {
    alert(s1+"_"+s2+"_"+s3);
}
myprint();      //undefined_undefined_undefined
myprint("string1","string2"); //string1_string2_undefined
myprint("string1","string2","string3","string4"); //string1_string2_string3
function myprint(s1,person) {
    var defaultperson = {   //默认person对象
        "name":"name1",
    "age":18,
    "sex":"female"
    };
    if(!s1) {    //s1不允许为空
        alert("s1 must be input!");
    return false;
    }
    person = person || defaultperson;  //接受person对象参数
    alert(s1+"_"+person.name+":"+person.age+":"+person.sex);
};

myprint(); //s1 must be input!
myprint("s1"); //s1_name1:18:female
myprint("s1",{"name":"sdcyst","age":23,"sex":"male"});  //s1_sdcyst:23:male
function myargs() {
    alert(arguments.length);
    alert(arguments[0]);
}
myargs();   //0  ---  undefined
myargs("1",[1,2]);  //2 --- 1
function(x) {
    if (x <= 1) return 1;
    return x * arguments.callee(x-1);
}  (section8.2)
var obj = {f0:function(){alert("f0");}}; //对象包含一个方法
function f1() {alert("f1");}
obj.f1 = f1;    //为对象添加方法

obj.f0(); //f0  f0是obj的方法
obj.f1(); //f1  f1是obj的方法
f1();     //f1  f1同时又是一个函数,可以直接调用
f0();     //f0仅仅是obj的方法,只能通过对象来调用
var obj = {"name":"NAME","sex":"female"};
obj.print = function() {  //为对象添加方法
    alert(this.name + "_" + this["sex"]);
};
obj.print();  //NAME_female
obj.sex = "male";
obj.print();  //NAME_male
var person = {name:"defaultname",
              setName:function(s){
              this.name = s;
          },
          "printName":function(){
              alert(this.name);
          }}
person.printName();       //defaultname
person.setName("newName");
person.printName();       //newName
function Person(name,sex) {
    this.name = name;
    this.sex = sex;
}
var per = new Person("sdcyst","male");
alert("name:"+per.name+"_sex:"+per.sex); //name:sdcyst_sex:male
function Person(name,sex) {  //构造函数
    this.name = name;
    this.sex = sex;
}
Person.prototype.age = 12;   //为prototype属性对应的prototype对象的属性赋值
Person.prototype.print = function() { //添加方法
    alert(this.name+"_"+this.sex+"_"+this.age);
};

var p1 = new Person("name1","male");
var p2 = new Person("name2","male");
p1.print();  //name1_male_12
p2.print();  //name2_male_12

Person.prototype.age = 18;  //改变prototype对象的属性值,注意是操作构造函数的prototype属性
p1.print();  //name1_male_18
p2.print();  //name2_male_18
function Person(name,sex) {  //Person类的构造函数
    this.name = name;
    this.sex = sex;
}
Person.prototype.age = 12;   //为Person类的prototype属性对应的prototype对象的属性赋值,
                             //相当于为Person类的父类添加属性
Person.prototype.print = function() { //为Person类的父类添加方法
    alert(this.name+"_"+this.sex+"_"+this.age);
};

var p1 = new Person("name1","male"); //p1的age属性继承子Person类的父类(即prototype对象)
var p2 = new Person("name2","male");

p1.print();  //name1_male_12
p2.print();  //name2_male_12

p1.age = 34; //改变p1实例的age属性
p1.print();  //name1_male_34
p2.print();  //name2_male_12

Person.prototype.age = 22;  //改变Person类的超类的age属性
p1.print();  //name1_male_34(p1的age属性并没有随着prototype属性的改变而改变)
p2.print();  //name2_male_22(p2的age属性发生了改变)

p1.print = function() {  //改变p1对象的print方法
    alert("i am p1");
}

p1.print();  //i am p1(p1的方法发生了改变)
p2.print();  //name2_male_22(p2的方法并没有改变)

Person.prototype.print = function() { //改变Person超类的print方法
    alert("new print method!");
}

p1.print();  //i am p1(p1的print方法仍旧是自己的方法)
p2.print();  //new print method!(p2的print方法随着超类方法的改变而改变)
function Person(name,age) {  //定义方法
    this.name = name;
    this.age = age;
}
var o = new Object();   //空对象
alert(o.name + "_" + o.age); //undefined_undefined

Person.call(o,"sdcyst",18); //相当于调用:o.Person("sdcyst",18)
alert(o.name + "_" + o.age); //sdcyst_18

Person.apply(o,["name",89]);//apply方法作用同call,不同之处在于传递参数的形式是用数组来传递
alert(o.name + "_" + o.age); //name_89
Person.counter = 0;   //定义类变量,创建的Person实例的个数
function Person(name,age) {
    this.name = name;
    this.age = age;
    Person.counter++; //没创建一个实例,类变量counter加1
};

Person.whoIsOlder = function(p1,p2) { //类方法,判断谁的年龄较大
    if(p1.age > p2.age) {
        return p1;
    } else {
        return p2;
    }
}

var p1 = new Person("p1",18);
var p2 = new Person("p2",22);

alert("现在有 " + Person.counter + "个人");  //现在有2个人
var p = Person.whoIsOlder(p1,p2);
alert(p.name + "的年龄较大");   //p2的年龄较大
function Circle(radius) {
    this.radius = radius;
    this.area = function() {
        return 3.14 * this.radius * this.radius;
    }
}
var c = new Circle(1);
alert(c.area());  //3.14
function Circle(radius) {
    this.radius = radius;
}
Circle.prototype.area = function() {
        return 3.14 * this.radius * this.radius;
    }
var c = new Circle(1);
alert(c.area());  //3.14
function Circle(radius) {  //定义父类Circle
    this.radius = radius;
}
Circle.prototype.area = function() { //定义父类的方法area计算面积
    return this.radius * this.radius * 3.14;
}

function PositionCircle(x,y,radius) { //定义类PositionCircle
    this.x = x;                    //属性横坐标
    this.y = y;                       //属性纵坐标
    Circle.call(this,radius);      //调用父类的方法,相当于调用this.Circle(radius),设置PositionCircle类的
                                   //radius属性
}
PositionCircle.prototype = new Circle(); //设置PositionCircle的父类为Circle类

var pc = new PositionCircle(1,2,1);
alert(pc.area());  //3.14
                   //PositionCircle类的area方法继承自Circle类,而Circle类的
                   //area方法又继承自它的prototype属性对应的prototype对象
alert(pc.radius); //1  PositionCircle类的radius属性继承自Circle类

/*
注意:在前面我们设置PositionCircle类的prototype属性指向了一个Circle对象,
因此pc的prototype属性继承了Circle对象的prototype属性,而Circle对象的constructor属
性(即Circle对象对应的prototype对象的constructor属性)是指向Circle的,所以此处弹出
的是Circ.
*/
alert(pc.constructor); //Circle    

/*为此,我们在设计好了类的继承关系后,还要设置子类的constructor属性,否则它会指向父类
的constructor属性
*/
PositionCircle.prototype.constructor = PositionCircle
alert(pc.constructor);  //PositionCircle
var sco = "global";  //全局变量
function t() { 
    var sco = "local";  //函数内部的局部变量
    alert(sco);         //local 优先调用局部变量
}
t();             //local
alert(sco);       //global  不能使用函数内的局部变量
function f(props) {
    for(var i=0; i<10; i++) {}
    alert(i);         //10  虽然i定义在for循环的控制语句中,但在函数
                      //的其他位置仍旧可以访问该变量.
    if(props == "local") {
        var sco = "local";
    alert(sco); 
    }
    alert(sco);       //同样,函数仍可引用if语句内定义的变量
}
f("local");      //10  local   local
var sco = "global";
function print1() {
    alert(sco);   //global
}
function print2() {
    var sco = "local";
    alert(sco);   //local
}
function print3() {
    alert(sco);   //undefined
    var sco = "local"; 
    alert(sco);   local
}

print1();  //global
print2();  //local
print3();  //undefined  local
function print3() {
    var sco;
    alert(sco);
    sco = "local";
    alert(sco);
}
var scope = "global"   //定义全局变量
function print() {
    alert(scope);
}
function change() {
    var scope = "local";  //定义局部变量
    print();              //虽然是在change函数的作用域内调用print函数,
                          //但是print函数执行时仍旧按照它定义时的作用域起作用
}
change();    //golbal
var i = 0;
function getNext() {
    i++;
    return i;
}
alert(getNext()); //1
alert(getNext()); //2
alert(getNext()); //3
function temp() {
    var i = 0;
    function b() {
        return ++i;
    }
    return b;
}
var getNext = temp();
alert(getNext());    //1
alert(getNext());    //2
alert(getNext());    //3
alert(getNext());    //4
function Person(name, age) {  
    this.getName = function() { return name; };  
    this.setName = function(newName) { name = newName; };  
    this.getAge = function() { return age; };  
    this.setAge = function(newAge) { age = newAge; };  
}  
  
var p1 = new Person("sdcyst",3);  
alert(p1.getName());  //sdcyst  
alert(p1.name);       //undefined   因为Person('类')没有name属性  
p1.name = "mypara"    //显示的给p1添加name属性  
alert(p1.getName());  //sdcyst     但是并不会改变getName方法的返回值  
alert(p1.name);       //mypara     显示出p1对象的name属性  
p1.setName("sss");    //改变私有的"name"属性
alert(p1.getName());  //sss  
alert(p1.name);       //仍旧为mypara  

 Ref:Javascript研究小组知识库

posted @ 2012-11-23 15:05  Nick.Chung  阅读(186)  评论(0编辑  收藏  举报