zoukankan      html  css  js  c++  java
  • js精要之函数

    数组排序

      var arr = [1,6,3,5,7,2];
        arr.sort(function(a,b){
            return a-b;
        });
        console.log(arr);

     arguments 参数存储对象

      function ref(value){
            return value;
        }
        console.log(ref("hi!"));
        console.log(ref("hi!",25));
        console.log(ref.length);
        ref = function(){
            return arguments[0];
        }
        console.log(ref("hi!"));
        console.log(ref("hi!",25));
        console.log(ref.length);

    函数sum计算数据和

      function sum(){
            var result = 0;
            for(var i=0;i<arguments.length;i++){
                result+= arguments[i];
            }
            return result;
        }
        console.log("sum() - "+sum());

    // 类重载

     function showa(){
            if(arguments.length === 0){
                console.log("没有参数");
            }else if(arguments.length === 1){
                console.log("一个参数:"+arguments[0]);
            }
        }
        showa();

    字面形式对象调用公共方法

    function sayNameForAll(){
            console.log(this.name)
        }
        var person1 = {
            name:"zs1",
            sayName:sayNameForAll
        }
        var person2 = {
            name:"ls2",
            sayName:sayNameForAll
        }
        var name = "ww3";
        person1.sayName();
        person2.sayName();
        sayNameForAll()

    // call();参数1指定this的值,参数2 传给函数的参数

    function sayNameForAlla(label){
            console.log(label+":"+this.name)
        }
        var person1a = {
            name:"zs1"
        
        }
        var person2a = {
            name:"ls2"
            
        }
        var name = "ccc"
        sayNameForAlla.call(this,"name")
        sayNameForAlla.call(person1a,"person1a")

    // apply()

    sayNameForAlla.apply(this,["namea"])
    sayNameForAlla.call(person1a,["person1a"])

    // 判断对象中是否含有属性 in (包括原型属性)

      console.log("name" in person2a);    //true
        console.log("toString" in person2a); //true

     // 判断对象中是否含有属性 hasOwnProperty() (不包括 原型属性) toString()是所有对象都具有的原型属性

      console.log(person2a.hasOwnProperty("name"));
        console.log(person2a.hasOwnProperty("toString"));
        // false

    // delete 删除属性

        delete person1a.name;
        console.log("name" in person1a); 
  • 相关阅读:
    分布式存储之GlusterFS
    应用中间件服务-weblogic
    生产环境tomcat升级新版本(tomcat漏洞修复)
    代码版本管理工具-gitlab
    链路追踪-skywalking
    去年前端校招面试的一些经验(阿里美团爱奇艺)
    mantis提交问题时报APPLICATION ERROR #27异常
    mantis无法上传附件,core/file_api.php: 1023异常
    centos7.2下安装mantis2.19.0
    linux常用命令
  • 原文地址:https://www.cnblogs.com/pangzi666/p/5190513.html
Copyright © 2011-2022 走看看