JS学习之旅(十)对象

本文深入探讨JavaScript中对象的创建方法,包括对象字面量、系统构造函数、自定义构造函数及Object.create方法。同时,讲解了对象的查询、修改、添加和删除操作,并分析了构造函数内部的工作原理。

对象

  1. 对象创建
    • var obj = {} plainObject 对象字面量/对象直接量
    • 构造函数
      • 系统自带的构造函数 new (Object();Array();Number();Boolean();String();Date()?
      • 自定义
    • Object create(原型)方法
	// 1.对象字面量
    var MrMing = {
        name : "xiaoMing",
        age : 30,
        sex : "male",
        health : 100,
        smoke : function () {
            console.log("I am smoking !");
            this.health -- ;
        },
        drink : function() {
            console.log("I am drink");
            this.health ++;
        }

    }
	
	// 2.系统自带构造函数
	var MrMing = new Object();
	
	// 3. 自定义构造函数
	// 构造函数,命名规则:大驼峰式命名
	function Car(color) {
		this.color = color;
		this.name = 'BMW';
		this.height = '1400';
		this.lang = '4900';
		this.weight = 1000;
		this.health = 100;
		this.run = function () {
			this.health --;
		}
		
	}
	var car= new Car('red');
	
	var car1 = new Car('green');
	
	car.name = 'Merz';
	car1.name = 'Maserati';
	car.run();
	car1.health;



	function Student(name, age, sex){
		this.name = name;
		this.age = age;
		this.sex = sex;
		this.grade = 2017;
	}
	var xiaomingStudent = new Student('小明', 18, '女');
	
  1. 对象查
	MrMing.name; 
  1. 对象改
	MrMing.name = "xiaowang";
  1. 对象增
	MrMing.wife = "xiaoli";
  1. 对象删
	delete MrMing.name;

构造函数内部原理

  1. 在函数体最前面隐士的加上this = {}
  2. 执行this.xxx = xxx;
  3. 隐式的返回this
    待完善…
	// new 之后...
	function Student(name, age, sex){
		// 1. var this = {};
		// AO {this : {name : "xiaoming", age : 18}}
		// 2.
		this.name = name;
		this.age = age;
		this.sex = sex;
		this.grade = 2017;
		// 3. return this;
	}

	var student = new Student('xiaoming', 18, 'nan');

	// 模拟
	function Pserson(name, heigth) {
		var that = {};
		that.name = name;
		that.height = height;
		return that;
		
	}
	var person = Pserson('xiaowang', 160);
	var person1 = Pserson('xiaoli', 180);
	
	

	// 如果不隐式返回,而是显示返回,返回的只能是兑现,否则系统不认
	function Student(name, age, sex){
		// 1. var this = {};
		// AO {this : {name : "xiaoming", age : 18}}
		// 2.
		this.name = name;
		this.age = age;
		this.sex = sex;
		this.grade = 2017;
		// return {}
		return 123;
	}

	var student = new Student('xiaohuang', 18, 'nan');	
	// 调用
	student.name // xiaohuang

包装类

	var num = new Number(123);
	var str = new String('abcd');
	var bol = new Boolean('true');


	// 原始值是没有属性的
	var num = 4;
	num.len = 3;
	// 隐式操作
	// new Number(4).len = 3; -->delete
	console.log(num.len);//undefined
	// new Number(4).len -->undefined

	// 测测
	var str = 'abcd';
	str.length = 2;
	// new String('abcd').length = 2; delete
	console.log(str);// abcd
	// new String('abcd').length
	console.log(str.length);// 4

小测试

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

小测试

    function employee(name, code) {
        this.name = 'wangli';
        this.code = 'A001';
    }

    newemp = new employee('zhangming', 'A002');
    document.write("雇员姓名:" + newemp.name +'<br>');// wangli
    document.write("雇员代号:" + newemp.code +'<br>');// A001
	// 闭包加对象创建
    function Person(name, age, sex){
        var a = 0;
        this.name = name;
        this.age = age;
        this.sex = sex;
        function sss(){
            a++;
            document.write(a);

        }
        this.say = sss;
    }

    var oPerson = new Person();
    oPerson.say(); // 1
    oPerson.say(); // 2
    var oPerson1 = new Person();
    oPerson1.say(); // 1
    var x = 1, y = z = 0;
    function add(n) {
        return n = n + 1;
    }
    y = add(x);
    function add(n) {
        return n = n + 3;
    }
    z = add(x);

    // 第二个add 会覆盖第一个add
    console.log(x);// 1
    console.log(y);// 4
    console.log(z);// 4
// 说出alert的值
    function b(x, y, a) {
        a = 10;
        alert(arguments[2]);// 10
    }
    function b (x, y, a) {
        arguments[2] = 10;
        alert(a);// 10
    }

    b(1, 2, 3);
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值