js一些基础知识整理

写了很久js,却对js的基础了解不多,稍微整理一下,留用

    /*************js对象属性特性(在js中类(Object)中的属性有4个描述起行为的特性)*******************/
    var person = {}
    Object.defineProperty(person, 'name', {
        configurable: false,//能否使用delete、能否修改属性特性、或能否修改访问器属性,false为不可重新定义,默认值为true
        enumerable: false,//对象属性是否可通过for-in循环,flase为不可循环,默认值为true
        writable: false,//对象属性是否可修改,flase为不可修改,默认值为true
        value: 'xiaoming' //对象属性的默认值,默认值为undefined
    });
    //value
    console.log(person);//xiaoming,默认value

    //writable
    person.name = "qiang";
    console.log(person);//xiaoming,不可修改value

    //enumerable
    for (var i in person) {
        console.log(person[i]) //无结果,不可循环
    }

    //configurable
    delete person.name
    console.log(person.name)//xiaoming,不可删除

    Object.defineProperty(person, 'name', {
        configurable: true //不可修改,将抛出错误
    });

    /*************** with语句的作用是将代码的作用域设置到一个特定的作用域中,但是不能用来给对象添加属性***************/
    with (expression) statement;
    //  比如:定义一个对象
    var a = {
        name: '张三',
        sex: '女'
    };
    //    一般的调用方式
    console.log(a.name);
    console.log(a.sex);
    //    使用with之后的调用
    with (a) {
        console.log(name);
        console.log(sex);
    }

    /************************************arguments  caller    callee***************************************/

//    arguments:  在函数调用时, 会自动在该函数内部生成一个名为 arguments的隐藏对象。 该对象类似于数组, 但又不是数组。
//    可以使用[]操作符获取函数调用时传递的实参。可模拟java中的重载,跟踪当前函数的参数传递情况(实参)
//    只有函数被调用时,arguments对象才会创建,未调用时其值为null
//    [function.]arguments[n]
//    参数function :选项。当前正在执行的 Function 对象的名字。 n :选项。要传递给 Function 对象的从0开始的参数值索引。
    function testArg() {
        alert("real parameter count: " + arguments.length);
        for (var i = 0; i < arguments.length; i++) {
            alert(arguments[i]);
        }
    }
    testArg(11);  //count: 1

    testArg('hello', 'world');  // count: 2

arguments的妙用:

[].slice.call(arguments)<=>Array.slice.call(arguments): 目的是将arguments对象的数组提出来转化为数组
    //    caller:  在一个函数调用另一个函数时,被调用函数会自动生成一个caller属性,指向调用它的函数对象。
    //    如果该函数当前未被调用,或并非被其他函数调用,则caller为null。
    function testCaller() {
        var caller = testCaller.caller;
        console.log(caller);
    }
    function aCaller() {
        testCaller();
    }
    aCaller();                              //result: function aCaller() { testCaller(); }

    function f1() {
        console.log(f1.caller);
        console.log(f1.arguments);
    }
    f1();                                    //  函数未被调用,结果为空

    //    callee:  当函数被调用时,它的arguments.callee对象就会指向自身,也就是一个对自己的引用。
    //    由于arguments在函数被调用时才有效,因此arguments.callee在函数未调用时是不存在的(即null.callee),引用它会产生异常
    function aCallee(arg) {
        console.log(arguments.callee);
    }
    aCallee();                              //result: function aCallee(arg) {   console.log(arguments.callee);   }

    /************************************apply  call***************************************/

    下面的文章对apply和call有比较详细的介绍,留用

    https://blog.csdn.net/bao19901210/article/details/21614761

    上述链接提出了apply的妙用,觉得比较常用: 

var arr1=new Array("1","2","3"); 
var arr2=new Array("4","5","6"); 
Array.prototype.push.apply(arr1,arr2);
结果: arr1: ["1", "2", "3", "4", "5", "6"]





  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值