• 面向对象的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
  • 相关阅读:
    supervisor使用小记
    linux新增定时脚本
    page_fault_in_nonpaged_area异常解决方案(已解决)
    和安卓对接老是ping不通?试试内网映射
    github文件下载加速器
    mybatis新增账号并且返回主键id
    arraylist源码解析
    MySQL安装教程
    通过get方法的方式获取配置项信息
    @Inject注解
  • 原文地址:https://www.cnblogs.com/SunShineM/p/6081485.html
Copyright © 2020-2023  润新知