对象的调用方式,对象的属性删除方式,构造器函数,apply和call以及arguments.callee // 调用对象的属性可以使用'.'也可以使用[]的方法调用 var book = { name :'Catch-22', published:1961, author:{ first
// 调用对象的属性可以使用'.'也可以使用[]的方法调用
var book = {
name :'Catch-22',
published:1961,
author:{
firstname:'Joseph',
lastname:'Heller'
}
}
console.log(book['author']['lastname'])
console.log(book['author'].lastname)
// 可以通过delete来删除hero.name这个属性
var hero = {};
hero.breed = 'turtle';
hero.name = 'Leonardo';
hero.sayName = function(){return hero.name;};
delete hero.name
// 构造器函数
function Hero(name){
this.name = name;
this.occupation ='Ninja';
this.whoAreYou = function(){
return "我是"+this.name +"我是一个 "+this.occupation;
}
}
var hero1 = new Hero('郑闪闪');
hero1.whoAreYou();
var h = Hero('Lenardo');
typeof h; //undefined 这个没有使用new,所以h就是Hero的函数函数值,由于
// Hero没有函数返回值,而默认的返回值是undefined,所以h的值为undefined
// caller属性,这个属性会返回一个调用该函数对象的外层函数引用
function A(){return A.caller;}
function B(){return A();}
// 由于我们在调用say()函数的对象方法call()时传递了两个参数,对象my_obj和'Dude,扎样一来,
// 当say()别调用时,其中的this被自动设置成了my_obj对象的引用
// apply() 和call()的工作方式基本相同,唯一不同之处在于参数的传递形式
var some_obj ={
name:'Nnja',
say:function(){
return 'I am' +this.name;
}
}
my_obj={name:'Scripting guru'};
some_obj.say.call(my_obj,'Dude'); //"I amScripting guru"
// arguments.callee,该属性引用的是当前被调用的函数对象
(function(count){
if(count <5){
alert(count);
arguments.callee(++count);
}
})(1)
split()
var csv = 'one,two,three,four';
csv.split(','); //["one", "two", "three", "four"]
