我爱编程

JS高级 04

2018-05-25  本文已影响0人  _MARPTS

基本包装类型

var str1 = new String('demo');
console.log(typeof str1)// object

var str2 = String('deme');
console.log(typeof str2)//string

var str3 = 'demo';
console.log(typeof str3)//string
var num1 = new Number(10)//object
var num2 = Number(20)//number
var num3 = 10 // number

var bool1 = new Boolean(true)//object
var bool2 = Boolean(true);//boolean
var bool3 = true// boolean

var str4 = new Object('demo');
var num4 = new Object(10);
var bool4 = new Object(true);

Object.prototype 的属性和方法

静态成员和实例成员
function Person(){
    this.name = '默认';
    var age = 10; //不是实例成员
}
Person.prototype.des ='des';

var p1 = new Person();

// 静态成员
Person.des = '描述函数';
Person.log = function () {
    console.log('log');
}
Object的静态成员(属性和方法)

这个方法比 Object.seal 更绝,冻结对象是指那些不能添加新的属性,不能修改已有属性的值,不能删除已有属性,以及不能修改已有属性的可枚举性、可配置性、可写性的对象。也就是说,这个对象永远是不可变的.

-  Object.isFrozen 判断一个对象是否是冻结的

Function构造函数的使用

var fun = new Function('a','b','console.log(a+b);')
fun3(1,2);
处理函数参数过长的问题(Function构造函数使用)
函数的隐藏参数
function  fun () {
    console.log(arguments[0]);
       
}
callee 和caller
// 使用arguments.callee匿名函数的递归调用
求1到n的和
(function (n){
    if(n ==1 ){
        return 1;
    }
    return arguments.callee(n-1)+n
}
)(10);
Function小应用(数组去重和求最大值)

console.log(fun(9, 12, 3, 4, 5, 3, 5, 2, 3));
```

==Function.prototype原型链==
Object和Function 的关系
私有变量和私有函数
function Person() {
    this.name = 'zs';
    var age = 20;
    var logAge = function(){
        console.log(age);
    }
    //特权方法:可以访问私有变量和私有函数的实例方法
    this.test = function(){
        return age;
    }
    
    this.test2 = function(){
         logAge() ;
    }
    
    var p1 = new Person();
    console.log(p1.text());
    p1.text2();
}
eval函数
with简单说明
    var obj = {
        name:'zs',
        age:20,
        des:'des'
    }
    with(obj){
        name= 'ls';
        age = 21;
        des = '22';
        obj.log = 'log';
    }
==函数内部this的指向==
    var obj = {
        name: 'zs',
        showName:function (){
            console.log(this);
        }
    }
    
    obj.showName();// this -->obj
    
    var fun = obj.showName;
    fun(); // this -> window
    
    function Person() {
        console.log(this);//p1    
    }
    
    var p1 = new Person;
    var obj = {
        age :20,
        show: function(){
            console.log(this.age)
        }
    }
    
    obj.show();
    
    var fun = obj.show;
    fun(); // this丢失问题undefined
 调用document.getElementById这个方法时,函数内部的this始终绑定document
var getEEle = (function(func){
    return function(){
        return func.apply(document,arguments);
    }
}
)(document.getElementById)

var div = getEle('demo');
console.log(div);
上一篇 下一篇

猜你喜欢

热点阅读