Js subject methods, the this pointer, the object
js objects
javascript in everything is an object (an array of characters, numbers, Boolean, regular, date).
example1
var obj = new Object();//构造函数写法
obj.name = 'xiaohong';
obj.sex = 'female';
obj.say = function(){console.log("hello")}
console.log(obj.name);//xiaohong
console.log(obj['name']);//xiaohong
obj.say();//hello
var obj2 = {
name:'xiaoming',
'sex':'female',
say:function(){console.log('hello')}
}//字面量写法 json数据格式
console.log(obj2.name);//xiaoming 对象 + . + 属性名
console.log(obj2['name']);//xiaoming 对象 + [ + ' + 属性名 + ' + ]
obj = null; //释放没有引用指向的对象占用的内存。
console.log(typeof new String('123'));//object
js create a new object can be created or new by {}, which is more convenient to use.
example2
var obj = {
'name':'aa',
'sex':'male',
'e-mail':'[email protected]'
}
console.log(obj['e-mail']);//[email protected]
console.log(obj.e-mail);//报错
na = 'name';
console.log(obj[na]);
Object reference attribute with square brackets and similar points, but the bracketed references can refer to some of the special circumstances of the characters, such as spaces, minus signs. Point method and reference attributes can reference variables.
this pointer
this pointer 21 have said in the summary, just to make some additions. Remember, the this point who called who .
example3
var name = 'outer';
var obj = {
name:'inner',
log:function(){
function fn(){console.log(this.name)}
return fn;
},
log2:function(){
console.log(name);
console.log(this.name);
}
}
obj.log()();//outer
obj.log2();//inner outer
var log = obj.log2;
log();//outer outer 这里相当于window.log()。
Some methods of objects
example4
function Person(){}
Person.prototype.name = 'x';
var person = new Person();
person.sex = 'man';
console.log('name' in person);//true
console.log('sex' in person);//true
console.log(person.hasOwnProperty('name'));//false
console.log(person.hasOwnProperty('sex'));//true
delete person.sex;
delete person.name;
console.log(person.name);//x
console.log(person.sex);//undefined
example5
var data1 = {
a:1,
b:2
}
var data2 = {
c:3,
a:4
}
var data3 = {
d:5,
f:6
}
//合并对象重复的属性
function combine(target,source){
var args = arguments;
if(args.length === 1) return target;
var index = 1;
while(source = args[index++]){
for(var k in source) if(source.hasOwnProperty(k)){
target[k] = source[k];
}
}
return target;
}
console.log(combine({},data1,data2,data3));//{a: 4, b: 2, c: 3, d: 5, f: 6}
//使用Object的assign方法同样可以实现。
console.log(Object.assign({},data1,data2,data3));//{a: 4, b: 2, c: 3, d: 5, f: 6}
hasOwnProperty()
It is used to determine whether an object has a property of an object or give your name. But note that this method can not check the object's prototype chain has a property, the property must be a member of the object itself. And in
the prototype chain can check whether the object has the attribute. for...in
It can be used to traverse the object properties. properties and methods may be deleted delete object (attributes can not be deleted on the prototype chain).