Math、arguments、caller、克隆、call/apply、this、继承、

补充

Math.ceil(123.345)//向上取整,结果为124
Math.floor(123.345)//向下取整,结果为123
Math.random()//0~1开区间的随机数
toFixed(n)//保留n位小数
  • js可正常计算的局限是小数点前后16位

  • arguments
    arguments.callet --> 引用自身函数
var num = (function(n){
    if(n ==1){
        return 1;
    }
    return n * arguments.callee(n - 1);
}(3)) //6

func.caller --> 指被谁调用

function test(){
    console.log(arguments.callee);
    function demo(){
        console.log(arguments.callee);
    }
    demo();
}
test();//打印test函数自身,走到demo时打印demo函数自身

  • 克隆
//1.判断是不是原始值
//2.判断是数组还是对象
//3.建立对应的新数组或对象
function deepClone(Origin, Target){//深度克隆--自己生成对象而不是共享地址
    var Target = Target || {};
    toStr = Object.prototype.toString;
//Object.prototype.toString.call([]) : "[object Array]";
    arrStr = "[object Array]";
    for(var prop in Origin){
        if(Origin.hasOwnProperty(prop)){//为了避免拿原型链上的属性
            if(Origin[prop] !== 'null' && typeof(Origin[prop]) == 'object'){
                Target[prop] = toStr.call(Origin[prop]) == arrStr ? [] : {};
                deepClone(Origin[prop], Target[prop]);
            }else{
                Target[prop] = Origin[prop];
            }
        }
    }
}
  • 三目运算符 ? :
var num = 1 > 0 ? ("10" > 9 ? 1 : 0) : 2;//1

  1. 原始值存储方式是栈内存
  2. 原始值没有属性和方法
//包装类
var str = 'abc';
// console.log(new String('abc').length)
console.log(str.length);//3 //内部 ↑

var num = 123;
num.abc = 'abc';
//new Number(num).abc = 'abc'; -->delete
console.log(num.abc);//重新new Number(num) //undefined

var num = 123; --> 不可配置

经历var的操作所得的属性,window,这种属性叫不可配置属性。
不可配置属性delete无效。

var obj = {

}
obj.num = 123; --> 可配置
Object.create(prototype, definedProperty);

  1. 预编译 this --> window
  2. 谁调用 this --> 谁
  3. call apply
  4. 全局 this --> window
var name = 'window';
var obj = {
    name : 'obj',
    say : function(){
        console.log(this.name);
    }
}
obj.say();//obj
obj.say.call(window);//window
fun();//全局范围内执行,window
fun.call(obj);//obj

  • 闭包–内部函数被保存到外部
var obj = {};
function a(){
    var aa = 123;
    function b(){
        console.log(aa);
    }
    obj.fun = b;
}
a();//obj.fun()--123---形成闭包--内部函数被保存到外部

构造函数要new

function Person(){
    //var this = {} ---1
    this.name = 'abc';
    this.age  =  123;
    //return this  ---2
}
var person = new Person();// new 实现1、2,构造对象

如果只是实现函数内的语句,可以直接调用,此时this --> window


[] != []
{} != {}
String([]) + 1 == "1"
String({}) == "[object Object]"
1, 2 ——> return 2;
Number([]) == 0
Number(null) == 0

  • null

null在作比较时转换为Number 0
""同理

typeof null //object
typeof undefined //undefined
null === undefined //false
null ==  undefined //true
null === null //true
null ==  null //true
!null //true
isNaN(1 + null) //false
isNaN(1 + undefined) //true

undefined作比较时永远false;
undefined 是全局对象的一个属性,它是全局作用域的一个变量。undefined的初始值就是原始数据类型undefined。


继承

//圣杯模式
function inherit(Target,Origin){
    function B(){};
    B.prototype = Origin.protortype;
    Target.protortype = new B();
    //∵c.__proto__ ——> new B().__proto__ ——> Aa.prototype
    //要使c.__proto__指向自身,↓
    Target.protortype.constructor = Target;
    Target.protortype.uper = Origin.protortype;//真正继承自
}
function Aa(){}
function C(){}
C.prototype.ch = "L";
inherit(C,Aa);
var a = new Aa;
var c = new C;
//input: c.ch //output: "L"
//input: a.ch //output: undefined
字符串去重(字符串仅有[a-z]的字符组成)
var str = "aaaaifuygauyegfwqkn";
String.prototype.unique = function(){
    var len = this.length;
        str = "",
        obj = {}
    for(var i = 0; i < len; i ++){
        if(!obj[this[i]]){
            obj[this[i]] = 'abc';
            str+=this[i];
        }
    }
}

<!DOCTYPE html><!-- 标准模式 ,删去该行渲染模式变成怪异模式/混杂模式 -->
document.compatMode //查看浏览器渲染模式
//标准模式: 'CSS1Compat'
//怪异模式: 'BackCompat'
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值