1. 把对象作为参数传入function

var a = [1,2,3,4];
var b = [];


function arraycopy(/* array */ from, /* index */ from_start,
/* array */ to, /* index */ to_start,
/* integer */ length) {
  var j = to_start - 1;
  for(var i = from_start - 1; i < length; i++) {
    to[j] = from[i];
    j += 1;
  }
}


function copyArray(arrObj){
  arraycopy(arrObj.from,
            arrObj.from_start || 0,
            arrObj.to,
            arrObj.to_start || 0,
            arrObj.length) 
}


/*
console.log("Array a is: " + a);
console.log("Initial Array b is: " + b);

arraycopy(a, 1, b, 1, 4);

console.log("New Array is: " + b);
*/

console.log("Initial Array b is: " + b);


var o = {
  from : a,
  to : b,
  length : 4
};

copyArray(o);

console.log("New Array is: " + b);

2. try/catch/finally

function factorial(x) {
  if(x === undefined || x < 0) {
    throw new Error("x is incorrect, please put a positive value");
  }
  for (var i = 1; x > 1; x--) {
    i *= x;
  }
  return i;
}



try{
  var n = Number(prompt("Please enter a number", ""));
  var f = factorial(n);
  console.log(f);
}
catch(err) {
  alert(err);
}

 

 

相关文章:

  • 2021-09-30
  • 2022-03-10
  • 2021-08-04
  • 2022-12-23
  • 2021-10-16
  • 2021-11-22
  • 2021-06-27
  • 2021-12-16
猜你喜欢
  • 2021-04-07
  • 2022-03-04
  • 2021-10-09
  • 2021-10-12
  • 2021-07-27
  • 2021-12-02
  • 2021-08-20
相关资源
相似解决方案