渡一 10 对象,包装类

2.属性的增、删、改、查;
3.对象的创建方法;
1.字面量;
2.构造函数
系统自带: new Object();Array();Number();Boolean();String();Date()
自定义:function Person(){...}
3.object.create(原型)

eg.

var mrDeng = {
    name : "MrDeng",
    age : 40,
    sex : "male",
    health : 100,
    smoke : function(){
        console.log("I am smoking! cool!");
        this.health--;
    },
    drink : function(){
        console.log("I am drink");
        this.health++;
    }
}

传参

function Student(name,age,sex){
    this.name = name;
    this.age = age;
    this.sex = sex;
    this.grade = 2017;
}
var student = new Student("zhangsan", 18, "male");

构造函数内部原理
1.在函数体最前面隐式加上this={};
2.执行 this.xxx = xxx;
3.隐匿的返回this

function Student(name,age,sex){
    /*
        var this = {
            name : "",
            age : ""
        }

    */
    
    this.name = name;
    this.age = age;
    this.sex = sex;
    this.grade = 2017;

    //return this;
}
var student = new Student("zhangsan", 18, "male");

包装类

var num = 4;
num.len = 3;
//new Number(4).len = 3; delete
console.log(num.len); //undefined

var str = "abc";
str += 1;
var test = typeof(str);
if(test.length == 6){
    test.sign = "typeof的返回结果可能为String";
    //new String(test).sign = "xxx";
}
// new String(test).sign
document.write(test.sign); //undefined


var str = "abcd";
str.length = 2;
//new String("abcd").length = 2;delete

//new String("abcd").length
console.log(str.length);

练习

1.运行test()和new test()的结果分别是什么?
var a = 5;
function test(){
    a = 0;
    console.log(a);         //0
    console.log(this.a);    //5
    var a;  
    console.log(a);         //0
}
test(); //0 5 0

var a = 5;
function test(){
    a = 0;
    console.log(a);         //0
    console.log(this.a);    //this-->test{} this.a-->undefined
    var a;  
    console.log(a);         //0
}
new test(); //0 undefined 0

5.
function Person(){
    var a = 0;
    function sss(){
        a++;
        console.log(a);
    }
    this.say = sss;
}
var person = new Person();
person.say();   //1 这里是一个闭包
person.say();   //2
var person1 = new Person();
person1.say()   //1

4.下面这段JS代码运行后x,y,z的值分别是多少?
var x=1,y=z=0;
function add(n){
    return n=n+1;
}
y=add(x);
function add(n){ //这个add会覆盖上面的
    return n=n+3;
}
z=add(x)
//1 4 4

6.
function foo(){bar.apply(null,arguments)}
function bar(x){console.log(arguments)}
foo(1,2,3,4,5)  //[1,2,3,4,5]

function b(x,y,a){
    arguments[2] = 10;
    alert(a);   //10
}
b(1,2,3);

//改写
a = 10;
console.log(arguments[2]); //10; arguments和变量是映射关系,但不相等
原文地址:https://www.cnblogs.com/lisa2544/p/15413293.html