var a = {};
Object.defineProperty(a,'b',{
value:1,
configurable: false // 设置为不可配置
})
console.log(delete a.b)
console.log(delete Object.prototype)
var x = 1;
console.log(delete this.x);
console.log(delete x)
以上打印都为false
检测属性
in 运算符
in 运算符的左侧是属性名(字符串),右侧是对象。如果对象的自有属性或继承属性中包含这个属性则返回true。
var a = {b:1};
console.log('a' in window); // true 声明的全局变量'a'是window的属性
console.log('b' in a); // true 'b'是a的属性
console.log('toString' in a); // true a继承了toString属性
console.log('c' in a); // false 'c'不是a的属性
var a = {b:undefined};
console.log(a.b !== undefined); //false
console.log(a.c !== undefined); //false
console.log('b' in a); //true
console.log('c' in a); //false
var a = new A();
console.log(a.constructor == A) //true
hasOwnProperty
可以用来判断某属性是不是这个构造函数的内部属性(不包括继承的)
语法: obj.hasOwnProperty(prop) 返回Boolean
function A (){
this.b = 1;
}
var a = new A();
console.log(a.hasOwnProperty('b')); //打印true
console.log(a.hasOwnProperty('toString')); //toString是继承属性 打印 false
console.log(a.hasOwnProperty('hasOwnProperty')); //同上,打印false
// 定义构造函数
function C(){}
function D(){}
var o = new C();
o instanceof C; // true,因为 Object.getPrototypeOf(o) === C.prototype
o instanceof D; // false,因为 D.prototype不在o的原型链上
o instanceof Object; // true,因为Object.prototype.isPrototypeOf(o)返回true
C.prototype instanceof Object // true,同上
toString
返回一个表示该对象的字符串
作用:
1、进行数字之间的进制转换
例如:var num = 255;
alert( num.toString(16) ); //结果就是'ff'
function Animal() {}
Animal.prototype.species = '动物';
function Dog(name, color) {
this.name = name;
this.color = color;
}
Dog.prototype = Object.create(Animal.prototype,{
constructor: {
value: Dog
}
})
var hashiqi = new Dog('hashiqi','blackandwhite');
console.log(hashiqi.species); //动物
3、拷贝继承
浅拷贝
function Animal() {}
Animal.prototype.species = '动物';
function Dog(name, color) {
this.name = name;
this.color = color;
}
function extend(child, parent) {
var c = child.prototype;
var p = parent.prototype;
for(key in p) {
c[key] = p[key]
}
}
extend(Dog, Animal);
var hashiqi = new Dog('hashiqi', 'blackandwhite');
console.log(hashiqi.species) // 动物
var method = 'say';
class Dog {
constructor (name,color) {
this.name = name;
this.color = color;
}
//注意,两个属性之间跟对象不同,不要加逗号,并且类的属性名可以使用变量或者表达式,如下
[method] () {
console.log('汪汪');
}
}
console.log(typeof Dog); // function 类的数据类型就是函数
console.log(Dog === Dog.prototype.constructor); // true 类本身就是构造函数
既然是构造函数,所以在使用的时候,也是直接对类使用new命令,跟构造函数的用法完全一致。
var hashiqi = new Dog('hashiqi', 'blackandwhite');
console.log(hashiqi.color); // blackandwhite
//上面采用表达式声明的类的属性可以用一下两种方式调用
hashiqi[method](); // 汪汪
hashiqi.say(); // 汪汪
注意:
1、先声明定义类,再创建实例,否则会报错
class 不存在变量提升,这一点与ES5的构造函数完全不同
new Dog('hashiqi','blackandwhite')
class Dog {
constructor (name,color) {
this.name = name;
this.color = color;
}
}
//Uncaught ReferenceError: Dog is not defined
//上面代码,Dog类使用在前,定义在后,因为ES6不会把类的声明提升到代码头部,所以报错Dog没有定义。
class Dog {
constructor (name,color) {
this.name = name;
this.color = color;
}
}
Dog(); // Uncaught TypeError: Class constructor Dog cannot be invoked without 'new'
class Dog {
constructor (name,color) {
this.name = name;
this.color = color;
}
//定义了两个constructor,所以会报错
constructor () {
}
}
new Dog('hashiqi', 'blackandwhite')
//Uncaught SyntaxError: A class may only have one constructor
Class表达式
与函数一样,类可以使用表达式的形式定义。
const Hashiqi = class Dog {
constructor (name,color) {
this.name = name;
this.color = color;
}
getName () {
//此处的Dog就是Dog构造函数,在表达式形式中,只能在构造函数内部使用
console.log(Dog.name);
}
}
var hashiqi = new Hashiqi('hashiqi', 'blackandwhite'); // 真正的类名是Hashiqi
var jinmao = new Dog('jinmao', 'yellow'); // 会报错,Dog没有定义
通常我们的表达式会写成如下,省略掉类后面的名称
const Hashiqi = class {
constructor (name,color) {
this.name = name;
this.color = color;
}
}
var hashiqi = new Hashiqi('hashiqi', 'blackandwhite');