日志文章

2007年10月25日 16:36:50

function 的使用详解

<script language="javascript">
//函数参数的活用
function list() {
  document.write("<UL>")
  for (var i=0; i<list.arguments.length; i++) {
    document.write("<LI>" + list.arguments)
   
  }
  document.write("</uL>")
}
list("U", "One", "Two", "Three")
//Caller的使用,在函数中
function myFunc() {
  if (myFunc.caller == null) {
    alert("The function was called from the top!")
  } else alert("This function's caller was " + myFunc.caller)
}


//prototype的使用
var array1 = new Array();
var array2 = new Array(3);
Array.prototype.description=null;
array1.description="Contains some stuff"
array2.description="Contains other stuff"
document.write(array1.description)
var s1 = new String("a")
var s2 = new String("b")
var s3 = new String("c") // Create a repeat-string-N-times method for all String objects
function str_rep(n) {
var s = "", t = this.toString()
while (--n >= 0) s += t
return s
}
String.prototype.rep = str_rep // Display the results
document.write("<P>s1.rep(3) is " + s1.rep(3)) // "aaa"
document.write("<BR>s2.rep(5) is " + s2.rep(5)) // "bbbbb"
document.write("<BR>s3.rep(2) is " + s3.rep(2)) // "cc" // Create an alternate method and assign it to only one String variable
//函数作为对象使用
function Dog(name,breed,color,sex) {
  this.name=name
  this.breed=breed
  this.color=color
  this.sex=sex
}
theDog = new Dog("Gabby","Lab","chocolate","girl")
//arity的使用在javascript1.2
function addNumbers(x,y){
  document.write("length = " + arguments.length + "<BR>")
  z = x + y
}
document.write("arity = " + addNumbers.arity + "<BR>")
addNumbers(3,4,5)
</script>

Tags: function   prototype   caller   javascript  

类别: javascript |  评论(1) |  浏览(1709) |  收藏
一共有 1 条评论
1楼 三笑数码科技--电脑扩展主机Ne.. 2007年10月26日 16:02:34 Says:
good
发表评论