一.对象遍历属性
(1).链式操作
var sched={
wakeup:function(){
console.log('a');
return this;
},
wakeupa:function(){
console.log('za');
return this;
},
wakeupb:function(){
console.log('zza');
return this;
}
}
sched.wakeup().wakeupa().wakeupb()
return this把sched return出去所以可以继续往下点,这叫链式操作
(2).中括号访问属性、
var myLang={
No1:'HTML',
No2:'CSS',
No3:'JavaScript',
myStudyingLang:function(num){
console(this['No'+num])
}
}
myLang.myStudyingLang(2);
(3).'.'属性原理
obj.name ->obj['name']
(4).枚举
(5).for-in
var car={
brand:'Beanz',
color:'red',
lang:'5'
}
for(var key in car){
//console.log(car.key)访问不到
console.log(car[key])//这样可以
}
car.key 是访问不到的,因为Js会转换中括号访问方式就会变成car[‘key’]
(6).for-in(原型)
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]);
}
(7).hasOwnProperty
var obj={
name:'爱笑也',
age:18
}
function Car(){
this.brand='Benz';
this.color='red';
}
Car.prototype={
lang:5
}
Object.prototype.name='Object';
var car=new Car();
for(var key in car){
if(car.hasOwnProperty(key)){
console.log(car[key]);
}
}
(8).in
判断属性值是否存在,
var car={
brand:'Benz',
color:'red'
}
console.log('displacement' in car)//返回false
in是不排除原型属性的
function Car(){
brand='Benz',
color='red'
}
Car.prototype={
displacement:'3.0'
}
var car=new Car();
console.log('displacement' in car)//返回true
(8).instanceof()
instanceof 判断这个实例化对象是否为指定构造函数构建的
function Car(){}
var car =new Car();
console.log(car instanceof Car)//返回true
(9).instanceof()原型链
console.log(car instanceof Object);//true
console.log([] instanceof Array);//true
console.log([] instanceof Object);//true
console.log({} instanceof Object);//true;
(10).综合实例
var a=[];
console.log(a.constructor);//f Array(){[native code]}
console.log(a instanceof Array);//true
var str=Object.prototype.toString.call(a);//[object Array]
if(str==='[object Array]'){
console.log('是数组');
}else{
consloe.log('不是')
}
//第三种方法更改了this的指向,相当于是a调用了Object的ToString
二.this
(1).函数的this指向原理
function test(b){
this.d=3;//window.d=3
var a=1;
function c(){}
}
test(123);
console.log(this.d)
GO:{
test=function
}
AO:{
b:123
arguments:123
a:1
c:function
this:window
}
(2).构造函数的this指向原理
function Test(){
//当new的时候系统会隐式创建this
//var this={
// _prototype_:Test.prototype
// }
this.name='123';
}
var test=new Test();
GO:{
Test:function
test:{
name:123
_prototype_:Test.prototype
}
}
AO{
name:123
_prototype_:Test.prototype
}
(3).总结
三.caller_callee
(1).callee(与arguments配合使用)
function test(a,b,c){
console.log(arguments.callee.length);//3
console.log(test.length);//3
console.log(arguments.length);//2
}
test(1,2);
function test1(){
console.log(arguments.callee);
function test2(){
console.log(arguments.callee)
}
test2();
}
test1();
(2).用途
function sum(n){
if(n<=1){
return 1;
}
return n+sum(n-1);
}
var sum=(function (n){
if(n<=1){
return 1;
}
return n+arguments.callee(n-1);
})(100);
(3).caller()
test1();
function test1(){
test2();
}
function test2(){
console.log(test2.caller)
}