javascript function

JavaScript函数断言与作用域
本文探讨了JavaScript中函数断言的使用方法,包括如何验证函数定义、名称及作用域,通过实例展示了如何实现断言来确保代码的正确性和一致性。
function assert(value1, value2) {
  if (value1) {
    console.log(value2);
  }
}

function isNimble() {
  return true;
}
assert(typeof window.isNimble === "function", "is isNimbl() defined");
assert(isNimble.name === "isNimble", "isNimble() has a name");

var canFly = function() {
  return true;
};
assert(typeof window.canFly === "function", "canFly() defined");
assert(canFly.name === "", "canFly() has no name");

window.isDeadly = function() {
  return true;
};
assert(typeof window.isDeadly === "function", "isDeadly() defined");
assert(isDeadly.name === "isDeadly", "isDeadly() has a name");

function outer() {
  assert(typeof inner === "function", "inner() in scope before declaration");
  function inner () {}
  assert(typeof inner === "function", "inner() in scope after declaration");
  assert(window.inner === undefined, "inner() not in global scope");
}

outer();
assert(window.inner === undefined, "inner() stilll not in global scope");


window.wieldsSword = function swingsSword() { return true; };
assert(window.wieldsSword.name === 'swingsSword', "wieldSword's real name is swingsSword");
"is isNimbl() defined"
"isNimble() has a name"
"canFly() defined"
"canFly() has no name"
"isDeadly() defined"
"inner() in scope before declaration"
"inner() in scope after declaration"
"inner() not in global scope"
"inner() stilll not in global scope"
"wieldSword's real name is swingsSword"

 

转载于:https://www.cnblogs.com/Iwillknow/p/4030957.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值