AngularJS系列之JavaScript函数和对象

转载请注明来源:http://blog.csdn.net/caoshiying?viewmode=contents

这篇文章针对的是有2年以上编程经验的朋友们参考的,作参考资料之用,不从基础讲起。

在ES6之前,JavaScript没有class、extends、abstract这样的关键字。在支持ActiveX的浏览器中可以用注册的COM类型声明JavaScript的对象。

1.1.1. 对象字面量

var x={
    name:"李婷",age:22
};//一个对象,没有自有属性与方法
var y=[];//对象个数为0的数组,有数组的属性与方法


1.1.2. 用函数声明类

对于Java而言,函数的本质是一个代码块,有特定的入口和出口。对于JavaScript而言,除了这些,它还有声明类的对象的功能。当用函数声明类的对象时,类的构造函数是这个函数。

var People=function (name,age){
    this.name=name;
    this.age=age;
    function getAge(){
        returnthis.age;
    }
    function getName(){
        returnthis.name;
    }
    functionsetName(name){
        this.name=name;
    }
    functionsetAge(age){
        this.age=age;
    }
};


1.1.3. 用散列表声明匿名类的对象

示例工程:J31。代码范式如下:

    var student1={};
    var student2=newObject();
    var student3={
        name:"李婷",Age:22,getName:function() {
            returnthis.name;
        }
    };
    console.log(student1);
    console.log(student2);
    console.log(student3);
    console.log(student1==student2);
    console.log(student3.getName());


1.1.4. 检索与更新

直接使用成员变量的名称就可以检索。对象没有的成员变量可以在赋值的时候创建。更新的对象的成员名称已经存在的问题参见章节“多态”。示例工程:J32。

    var student1={
        name:"李婷",age:23,"first-name":"不告诉你","truth-age":22,getName:function(){
            returnthis.name;
        }
    };
    console.log(student1.name);
    console.log(student1["first-name"]);
    student1.name="李婷";
    student1["first-name"]={
        firstName:"李",fullName:"李婷"
    };
    student1["second-name"]="婷";
    student1.secondName="婷";
    console.log(student1.name);
    console.log(student1["first-name"]);
    console.log(student1["second-name"]);
    console.log(student1.secondName);


1.1.5. 引用(Reference)

对象通过引用来传递。语言自身不支持拷贝功能。示例工程:J33。

    var student1={
        name:"李婷",age:23
    };
    var x=student1;
    x.secondName="婷"
    console.log(student1.secondName);
    var a={},b={};
    console.log(a==b);
    a=b;
    console.log(a==b);
    var i={},j={},k={};
    console.log(i==j==k);
    i=j=k={};
    i=true;
    console.log(j==k);
    console.log(i==j==k);
    i=j=k={};
    k=true;
    console.log(i==j==k);//自左向右结合


1.1.6. 原型(Prototype)、反射(Reflection)与委托

每个类都连接到一个原型对象,并且它可以从中继承属性。所有通过对象字面量创建的对象都连接到Object.prototype这个JavaScript中的标准对象。

反射支持获取对象的数据类型,包括简单数据类型和复杂数据类型。typeof表达式可以获取对象的数据类型。函数本身也是一个数据类型,它的类型名称是"function"。

修改内置对象的方法是操作内置对象的prototype。对象的一个或多个继承关系以及根据继承次序依次获得的父类型的成员构成原型关系链。获取一个对象的某个成员且该对象没有此属性名称的时候,JavaScript引擎根据继承次序逆向寻找至根类型Object。如果找不到就返回undefined。这个过程称为委托。

null与undefined是不相同的。示例工程:J35。

    if(typeofObject.prototype.sayHello!=="function"){
        Object.sayHello=function(o){
            varF=function(){
                console.log("HelloWorld from Object.sayHello!");
            }
            F.prototype=o;
            return newF();
        }
    }
    varPeople=function(){
        this.name="李婷";
    };
    var person1=newPeople();
    console.log("People是什么:"+typeof People);
    console.log("person1类型名称:"+typeof person1);
    console.log("null与undefined是否相等:"+null==undefined);
    console.log(person1);
    console.log(person1.sayHello);//委托
    console.log(person1.prototype.sayHello);//出错
    console.log("你看不到这一行。把上面一行错误代码注释掉就可以了。");


1.1.7. 多态(Polymorphism)、枚举(Enumation)

for in语句可以遍历对象中的所有成员,包括变量和方法,但不包括继承自父类的以及复杂数据类型的成员。hasOwnProperty方法可用来判断当前对象是否存在自有方法,还有可用遍历数组。JS不支持重载和多态,但用户可以写多个名称相同的函数。当遇到多个名称相同的函数时,JS自动使用最后定义的函数。示例工程:J37。

    var student1={
        name:"李婷",age:22,courses:{
            culture:{
                id:"AAAAAAAA-AAAA-AAAA-AAAA-AAAAAAAAAAAA",teacher:"你1"
            }
        },marks:{
            culture:100
        },getName:function(){
            return"函数自带的方法:"+this.name;
        }
    };
    var goto=false;
    for(name instudent1){
        output://仅测试用,项目中不要这样干。
        if(student1.hasOwnProperty(name)){
            if(goto){
                console.log("继承的:"+name);
            }else{
                console.log("自有的:"+name);
            }
            if(typeofstudent1[name] == "function"){
                console.log("方法:"+name);
            }else{
                console.log("属性:"+name);
            }
            goto=false;
        }else{
            goto=true;
            breakoutput;
        }
    }
   varStudentClass=function(name,age){
       this.name=name;
       this.age=age;
 
       functionwriteMessage(){
           console.log("没有参数的writeMessage方法");
       }
 
       functionwriteMessage(tip){
           console.log("有一个tip参数的writeMessage方法");
           console.log(tip);
       }
 
       function writeMessage(tip,count){
           console.log("有一个tip参数和一个参数count的writeMessage方法");
           console.log(tip);
           console.log(count);
       }
 
       this.tell=function(){
           writeMessage();
           writeMessage("提示");
           writeMessage("提示",2);
       }
   }
 
    var student2=newStudentClass("李婷",22);
    student2.tell();


1.1.8. 删除对象的属性

delete运算符可以用来删除对象的成员变量,不能删除成员函数。

    varStudentClass=function(name,age){
        this.name=name;
        this.age=age;
        this.courses={
            culture:{
                id:"AAAAAAAA-AAAA-AAAA-AAAA-AAAAAAAAAAAA",teacher:"你1"
            }
        };
        this.marks={
            culture:100
        };
    };
    StudentClass.prototype.getName=function(reserved) {
        console.log(reserved+this.name);
        returnthis.name;
    }
    StudentClass.prototype.getAge=function(reserved) {
        console.log(reserved+this.age);
        returnthis.age;
    }
    var student1=newStudentClass("李婷1",22);
    var student2=new StudentClass("李婷2",22);
    console.log("删除前:");
    console.log(typeofstudent1.name);
    console.log(typeofstudent1.getName);
    console.log(typeofstudent2.name);
    console.log(typeofstudent2.getName);
    deletestudent1.name;
    deletestudent1.getName;
    console.log("删除后:");
    console.log(typeofstudent1.name);
    console.log(typeofstudent1.getName);
    console.log(typeofstudent2.name);
    console.log(typeofstudent2.getName);


1.1.9. 内置参数

函数声明不需要写明函数有多个少。函数没有声明参数有多少个时根据函数的内部逻辑决定传递参数个数。示例工程:J44。

    varstudent1=function(){
        document.write("参数个数:"+arguments.length);
        document.write("<br/>");
        document.write("<br/>");
        document.write("<br/>");
        document.write("我叫"+arguments[0]+",我今年"+arguments[1]+"岁了");
    }
    student1("李婷",22,100);

相关文章

ANGULAR.JS:NG-SELECTANDNG-OPTIONSPS:其实看英文文档比看中...
AngularJS中使用Chart.js制折线图与饼图实例  Chart.js 是...
IE浏览器兼容性后续前言 继续尝试解决IE浏览器兼容性问题,...
Angular实现下拉菜单多选写这篇文章时,引用文章地址如下:h...
在AngularJS应用中集成科大讯飞语音输入功能前言 根据项目...
Angular数据更新不及时问题探讨前言 在修复控制角标正确变...