JavaScript——对象

对象

数据属性

数据属性的四个行为特性:

  • Configurable 表示是否可以通过delete删除属性 是否能修改属性的特性 是否能把属性修改为访问属性 默认true
  • Enumerable 表示是否能通过for in 循环返回属性 默认true
  • Writable 表示是否能修改属性的值 默认true
  • Vaule 表示包含这个属性的值
    //修改属性的特性需要通过ES5的Object.defineProperty(属性所在对象,属性名称,描述对象)方法
	    var person = {};
	    Object.defineProperty(person, 'name', {
	    	configurable:true,
	    	// enumerable:true,
	    	writable: false,
	    	value: 'wjc'
	    })
	注:如果使用Object.defineProperty()定义一个属性  默认都是false

访问器属性

访问器属性,它有四个特性:

  • Configurable 表示是否能通过delete删除属性 是否能修改属性的特性 默认true
  • Enumerable 表示能否通过for-in循环返回属性 默认true
  • Get 在读取属性时调用的函数 默认undefined
  • Set 在写入属性时调用 默认undefined
   //访问器属性同样通过Object.defineProperty()定义
	 	Object.defineProperty(book, 'year', {
	 		get: function(){
	 			return this._year;
	 		},
	 		set: function(newValue){
	 			this._year = newValue;
	 			this.edition += newValue - 2004;
	 		}
	 	});

	//使用Object.defineProperty()同时定义多个属性
		Object.defineProperties(book,{
			//定义两个属性
			_year:{
				writable: true,
				value: 2004
			},
			edition:{
				writable: true,
				value: 1
			},
			//定义两个访问器属性
			year:{
				get: function(){
					return this._year;
				},
				set: function(newValue){
					if(newValue > 2004){
						this._year = newValue;
						this.edition += newValue - 2004;
					}
				}
			}
		});
	//即通过传对象的方式实现同时定义多个属性

	//ES5提供了Object.getOwnPropertyDescriptor()方法读取属性的特性
		var descriptor = Object.getOwnPropertyDescriptor(book, "_year");
		console.log(descriptor);

创建对象(类)

创建对象的六种模式

  • 工厂模式
  • 构造函数模式
  • 原型模式
  • 动态原型模式
  • 寄生构造函数模式
  • 组合使用构造函数和原型模式

这里只列举最常用的创建对象的方法(组合使用构造函数模式和原型模式)

    //不共享的放在构造函数
	//注意this声明的是公有属性  var声明的是私有属性
	function Person(name, age, job){
		this.name = name;
		this.age = age;
		this.job = job;
		this.friends = ['zff', 'cj'];
	}
	//将共享的放在原型中
	Person.prototype = {
		constructor: Person,
		sayName: function(){
			this.name;
		}
	}
	var person = new Person('wjc', 21, 'student');
	var person2 = new Person('aa', 21, 'student');

对原型操作的几个方法

  • isPrototypeOf() 判断实例与构造函数原型之间的关系
    Person.prototype.isPrototypeOf(person1)

  • Object.getPrototypedOf() 获得对象的原型
    Object.getPrototypeOf(person1) == Person.prototype

  • hasOwnProperty() 检测一个属性是否存在实例中 注意是实例!和原型没有关系
    person1.hasOwnProperty(“name”);

  • in操作符 判断属性是否存在原型中
    name in Object

  • Object.keys() ES5提供的方法 获取对象中的属性 返回一个数组

  • Object.getOwnPropertyNames() 作用和Object.keys()效果一样

    注:prototype也可以扩展JS中的内置对象 例如

	String.prototype.startWith = function(text){
			return this.indexOf(text) == 0;
	}

继承

可以通过prototype实现继承

	//定义一个动物类
	function Animal(name){
		//定义公有属性
		this.name = name || '';
		//定义公有方法
		this.sleep = function(){
			return this.name + '正在睡觉';
		}
	}

	//原型方法
	Animal.prototype.eat = function(food){
		return this.name + '正在吃' + food;
	}
	var animal = new Animal('fish');
	console.log(animal.name);
	console.log(animal.eat('屎'));
	//原型链继承
	console.log('原型链继承');

	//设置Cat的原型为Animal	父类的所有属性和方法都可以使用
	Cat.prototype = new Animal();
	Cat.prototype.name =   'cat';
	Cat.prototype.eat = function(food){
		return 'cat又吃' + food;
	}
	//实例Cat对象
	var cat = new Cat();
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值