对象内置方法,继承,instanceof添加上(安全使用)

来源:互联网 发布:太师鞭 知乎 编辑:程序博客网 时间:2024/06/10 23:45

instanceof添加上(安全使用)

<!DOCTYPE html><html lang="en">    <head>        <meta http-equiv="Content-Type" content="text/html; charset=utf-8">        <meta name="Author" content="">        <meta name="Keywords" content="">        <meta name="Description" content="">        <title>Document</title>        <style type="text/css">            * {margin: 0; padding: 0;}            a {text-decoration: none;}            ul,li {list-style: none;}            body {font-family: "Microsoft yahei";}        </style>    </head><body><script type="text/javascript">    /*        instanceof 检测创建的对象是由谁实例得到的 new 出来的        ####构造函数单独执行 是没有任何意义的        只有在new的时候才会有意义     */    // function Person(names,age){    //     this.names = names;    //     this.age = age;    // };    // Person.prototype.eat = function(){    //     console.log(this.names);    // };    // Person.prototype.type = "bitch";    // var p1 = new Person("pr",1213);    // console.log(p1.names+"====="+p1.age);    // 安全使用添加instanceof    function Person(name,age){        if(this instanceof Person){  //作防止措施.==>防止this指向window            this.names = name;            this.age = age;        }else{            return new Person(name,age);        }    };    // var p1 = Person("pr",123);  //可以不用new    var p1 = new Person("pr",123);    alert(p1.names)</script></body></html>

对象内置方法

        数据类型 object -- json                object        constructor             判断来自哪一个构造函数        in                      判断一个属性是否存在于对象中(不分是不是继承的属性或者方法)        hasOwnProperty          判断自有属性        isPrototypeOf           判断来自哪一个原型链        propertyIsEnumerable    属性是否可以被for in        toString                转成一个字符串        valueOf                 生成一个副本 引用地址的一样的
<!DOCTYPE html><html lang="en"><head>    <meta charset="UTF-8">    <meta name="Author" content=" ">    <title>Document</title>    <style type="text/css">        *{margin: 0;padding: 0;}        a{text-decoration: none;}        ul,li{list-style: none;}        body{font-size: 14px;font-family: "微软雅黑";}    </style></head><body>    <script type="text/javascript">        /*            数据类型 object -- json            object            constructor             判断来自哪一个构造函数            in                      判断一个属性是否存在于对象中(不分是不是继承的属性或者方法)            hasOwnProperty          判断自有属性            isPrototypeOf           判断来自哪一个原型链            propertyIsEnumerable    属性是否可以被for in            toString                转成一个字符串            valueOf                 生成一个副本 引用地址的一样的         */        function Person(name,age){            //自有属性            this.name = name;            this.age = age;            this.say = function(){                alert(1);            };        };        //扩展属性        Person.prototype.eat = function(){            alert("eat");        };        Person.prototype.type = "人类";        var p1 = new Person("xq",18);        var p2 = new Person("cc",20);        for(var k in Person.prototype){            console.log(Person.prototype[k]);        };        // alert(Person.prototype.constructor);        /*            in            hasOwnProperty            isPrototypeOf         */        // alert(p1.eat === p2.eat);//true        // alert(p1.say === p2.say);//false        // alert("eat" in p1);//true        // alert("say" in p1);//true        // alert(p1.hasOwnProperty("name"));//true     自己的        // alert(p1.hasOwnProperty("type"));//false    原型链        // alert(p1.hasOwnProperty("valueOf"));//false object        // alert(Person.prototype.isPrototypeOf(p1));//true        // var json1 = {        //     name:'xq'        // };        // var json2 = json1.valueOf();        // alert(json1 === json2);        // json2.name = "茅草屋";        // alert(json1.name);        // var date = new Date();        // var d = date.toLocaleString();        // document.write(d);    </script></body></html>

extend继承拷贝

<!DOCTYPE html><html lang="en">    <head>        <meta http-equiv="Content-Type" content="text/html; charset=utf-8">        <meta name="Author" content="">        <meta name="Keywords" content="">        <meta name="Description" content="">        <title>Document</title>        <style type="text/css">            * {margin: 0; padding: 0;}            a {text-decoration: none;}            ul,li {list-style: none;}            body {font-family: "Microsoft yahei";}        </style>    </head><body><script type="text/javascript">    /*        继承extends            子类可以继承父类            子类可以使用父类的方法或者属性            子类不可以影响父类(重写)     */    function Person(name){        if(this instanceof Person){            this.name = name;        }else{            return new Person(name);        }    };    Person.prototype.showName = function(){        alert(this.name);    };    var p = Person("goudan");    // p.showName();    function Child(name,age){        if(this instanceof Child){            this.age = age;            this.name = name;        }else{            return new Child(name,age);        }    };    // Child.prototype = Person.prototype;  //子类不可以影响父类    // Child.prototype = extend(Person.prototype);  //相当于字面量扩展  ===>会把constructor改变成Object    extend(Person.prototype,Child.prototype);    Child.prototype.showAge = function(){        alert(this.age);    };    var c = Child("ergou",18);    // c.showAge();    c.showName();    // alert(c.constructor);//Object    // p.showAge();//子类不可以影响父类    function extend(obj,childObj){  //正确的做法.        // var childObj = {};        for(var k in obj){            // 继承父类的属性,方法.   原型链的            childObj[k] = obj[k];        };        // return childObj;    };</script></body></html>

类继承

<!DOCTYPE html><html lang="en">    <head>        <meta http-equiv="Content-Type" content="text/html; charset=utf-8">        <meta name="Author" content="">        <meta name="Keywords" content="">        <meta name="Description" content="">        <title>Document</title>        <style type="text/css">            * {margin: 0; padding: 0;}            a {text-decoration: none;}            ul,li {list-style: none;}            body {font-family: "Microsoft yahei";}        </style>    </head><body><script type="text/javascript">    /*        继承extends            子类可以继承父类            子类可以使用父类的方法或者属性          !!做到子类不可以影响父类(重写)     */    function Person(name){        // if(this instanceof Person){            this.name = name;        // }else{        //     return new Person(name);        // }    };    Person.prototype.showName = function(){        alert(this.name);    };    var p = new Person("goudan");    function Fn(){    };    Fn.prototype = Person.prototype;//==> Fn的constructor=>Person    Child.prototype = new Fn();  //onstructor=>Person    Child.prototype.constructor = Child;//手动改回来.    function Child(name,age){        if(this instanceof Child){            this.age = age;            Person.call(this,name);        }else{            return new Child(name,age);        }    };    Child.prototype.showAge = function(){        alert(this.age);    };    var c = Child("ergou",18);    console.log(c.name);    // c.showAge();    c.showName();    alert(c.constructor)//构造器一层一层找    // p.showAge();</script></body></html>
0 0
原创粉丝点击