首页 > 代码库 > 面向对象的prototype

面向对象的prototype

  function Cat(){
            this.name=‘小白‘;
        }
        Cat.prototype.color=‘white‘;
        Cat.prototype.skill=function(){
            alert(‘吃鱼‘);
        }
        var cat1=new Cat();
        console.log(cat1.name);//小白
        console.log(Cat.prototype);//object
        console.log(cat1.prototype);//undefined
        console.log(cat1 instanceof Cat);//true
        console.log(cat1 instanceof Object);//true
        var obj=new Object();
        Object.prototype.abc=‘123‘;
        var str=‘abcd‘;
        var arr=[‘1‘,‘2‘,‘3‘];
        console.log(str.abc);//123
        console.log(arr.abc);//123
        String.prototype.len=function(){
            return this.length;
        }
        alert(str.len());
        console.log(str.__proto__);//String
    
    console.log(str.constructor);// String

     console.log(str.constructor.prototype.constructor);// String


        console.log(str.__proto__.__proto__);//Object
        Date.prototype.getWeek=function(){
            var arr=[‘周末‘,‘周一‘,‘周二‘,‘周三‘,‘周四‘,‘周五‘,‘周六‘];
            var index=this.getDay();//0-6
            return arr[index];
        }
        var date=new Date();
        console.log(date.getWeek());//周六
Array.prototype.quchong=function (){
        var arr=[];
        this.sort(function (a,b){return a-b;});
        for (var i = 0; i < this.length; i++) {
            if (this[i]==this[i+1]) {
                continue;
            };
            arr.push(this[i]);
        };
        return arr;
    }
    var arr1=[12,45,7,12,75,45,5,32,12];
    console.log(arr1.quchong());
function fn1(){
            var x=5;
            x++;
            alert(x);
        }
        fn1();//6
        fn1();//6
        fn1();//6
        fn1();//6
        function fn2(){
            var x=5;
            function fn3(){
                x++;
                alert(x);
            }
            return fn3;
        }
        var fn=fn2();
        fn();//6
        fn();//7
        fn();//8
        fn();//9

 

面向对象的prototype