当你在编写一个function,非方法,在javascript中,你可以通过with(object)的方式获取该方法分配给object后的对象引用
例如:
// Define a function which will work as a method
function addPrice(amount){
with(this){
price = amount;
}
}
function book(title, author){
this.title = title;
this.author = author;
this.price = 0;
this.addPrice = addPrice; // Assign that method as property.
}
使用with后作用域相当于book构造器里的作用域