为什么代码放在return语句之后,它会被执行吗?
Why the code is put after the return statement, will it get executed?
我不明白为什么Function.prototype.call()可以这样使用?据我所知,如果函数返回之后的代码将不会被执行。我是不是漏掉了什么?
function Product(name, price) {
this.name = name;
this.price = price;
if (price < 0)
throw RangeError('Cannot create product
"' + name + '" with a negative price');
return this;
}
function Food(name, price) {
Product.call(this, name, price); // if the function returns here why put this.category after this statement?
this.category = 'food'; // will this ever get executed?
}
Food.prototype = Object.create(Product.prototype);
var cheese = new Food('feta', 5);
我明白Product, Food都是构造函数,我们可以使用调用链构造函数来调用对象,类似于Java。但是为什么不把语句
this.category = 'food';
之前Product.call(this, name, price);
据我所知,如果函数返回之后的代码将不会被执行。
是的。
我错过了什么吗?
return
在本地工作,只结束当前函数调用。
.category
属性前,Food
功能不设置return
。顺便说一句,Product
中的return
是不必要的,因为构造函数不需要显式返回
相关文章:
- 未执行Ajax循环的Javascript之后的代码
- 如何在`window.open`之后执行回调
- 在$state.go之后执行$window.location.reload(true)
- Javascript:在每个onmouseover事件之后执行2个函数
- 可以使 jquery 点击事件在所有其他点击事件之后执行
- 如何在调用去抖函数之后和执行之前取消该函数
- 在两个元素的 onrender 事件之后执行函数
- 流星技术/模式,用于等待数据库变量更改,然后在 in 之后执行某些操作
- 在jQuery .load Ajax之后执行if-statement
- Javascript:在css动画之后继续执行
- ASPxClientGridView之后激发的客户端事件.ApplyFilter执行其工作
- 浏览器在“打开”之后继续执行Javascript;PageUnload”&新的“;PageLoad”;
- 使javascript在firefox中的插件javascript之后执行
- 在catch之后执行
- 是否可以在特定的console.log()条目之后执行Javascript函数
- PHPOx-GetBlock-Javascript在getContent之后不执行
- oncomplete 方法并不总是在操作 JSF 之后执行
- 如何仅在第一行代码执行之后执行下一行代码
- 将表单captcha重置延迟到submit()执行之后
- 在控制器中的所有异步执行之后调用“渲染”的正确方法是什么