function Car() {
this.brand = 'Benz';
this.color = 'red';
this.displacement = '3.0';
}
Car.prototype = {
lang: 5,
width: 2.5
}
Object.prototype.name = 'Object';
var car = new Car();
for(var key in car) {
console.log(key + ': ' + car[key]);
}
function Car() {
this.brand = 'Benz';
this.color = 'red';
this.displacement = '3.0';
}
Car.prototype = {
lang: 5,
width: 2.5
}
Object.prototype.name = 'Object';
var car = new Car();
for(var key in car) {
if(car.hasOwnProperty(key)) {
console.log(key + ': ' + car[key])
}
}
判断属性是否在对象内(原型上的也算)
var car = {
brand: 'Benz',
color: 'red'
}
console.log('displacement' in car);
console.log('color' in car);
function Car() {
this.brand = 'Benz';
this.color = 'red';
this.displacement = '3.0';
}
Car.prototype = {
lang: 5,
width: 2.5
}
var car = new Car();
console.log('width' in car);
判断左边的A是否是通过右边的B构造出来
function Car() {}
var car = new Car();
function Person() {}
var p = new Person();
function Student() {}
Student.prototype = p;
var stu = new Student();
console.log(car instanceof Car);
console.log(p instanceof Car);
console.log([] instanceof Array);
console.log(car instanceof Object);
console.log([] instanceof Object);
console.log({} instanceof Object);
console.log(stu instanceof p);
var a = [];
console.log(a.constructor === Array);
console.log(a instanceof Array);
var str = Object.prototype.toString.call(a);
console.log(str == '[object Array]');