1. 程式人生 > >javaScript的一些屬性

javaScript的一些屬性

<html>
<head>
<script type="text/javascript">
function ArgTest(){
  var i, s, numargs = arguments.length;
  s = numargs;  
  if (numargs < 2)
 s += " argument was passed to ArgTest. It was ";
  else
 s += " arguments were passed to ArgTest. They were " ;
  for (i = 0; i < numargs; i++)
 {
s += arguments[i] + " ";
 }
  return(s);
}
//測試函式傳入的引數的個數
function result()
{
var re = ArgTest(1,2);
alert('Arguments result:' + re);
}
//測試呼叫的函式體
function callLevel()
{
var callLe = result.caller;
alert(callLevel);
}
//兩個方法名相同的方法,回自動適配寫在後面的一個
function consta()
{
var con = new String("11").constructor;
alert("con:"+con);
}
function consta()
{
var con = new String("11").constructor;
alert("consta:"+con);
}
//測試E,以及顯示函式和隱式函式
var aa = function newVar()
{
alert(Math.E);

//測試global屬性
function RegExpPropDemo(flag){
  if (flag.match(/[^gim]/))        //檢查標誌的有效性。
return("Flag specified is not valid");
  var r, re, s                    //宣告變數。
  var ss = "The man hit the ball with the bat.\n";
  ss += "while the fielder caught the ball with the glove.";
  re = new RegExp("the",flag);    //指定要查詢的樣式。
  alert("re"+re);
  r = ss.replace(re, "a");        //用 "a" 替換 "the"。
  s = "Regular Expression property values:\n\n"
  s += "global  ignoreCase  multiline\n"
  if (re.global)                  //測試 global 標誌。
s += " True     ";
  else
s += "False     ";
  if (re.ignoreCase)              //測試 ignoreCase 標誌。
s += " True  ";
  else
s += "False  ";
  if (re.multiline)               //測試 multiline 標誌。
s += "     True     ";
  else
s += "     False   ";
  s += "\n\nThe resulting string is:\n\n" + r;
  alert(s);
  return(s);                      //返回替換字串。
}

function ownProperty()
{
var s = new String("JScript");
alert(s.hasOwnProperty("split"));
alert("string prototype:"+String.prototype);
alert(String.prototype.hasOwnProperty("split"));
}
function RegExpTest(){
 var ver = Number(ScriptEngineMajorVersion() + "." + ScriptEngineMinorVersion())
 alert(ver);
 if (ver >= 5.5){
var src = "The rain in Spain falls mainly in the plain.";
var re = /\w+/g;
var arr;
while ((arr = re.exec(src)) != null)
  alert(arr.index + "-" + arr.lastIndex + "\t" + arr);
 }
 else{
alert("You need a newer version of JScript for this to work");
 }
}
//input的屬性
function inputDemo(){
  var s;
  var re = new RegExp("d(b+)(d)","ig");
  var str = "cdbBdbsbdbdz";
  var arr = re.exec(str);
  s = "The string used for the match was " + RegExp.input; 
  alert(s);
  return(s);
}
//length Array
function LengthDemo(){
  var a;
  a = new Array(0,1,2,3,4);
  alert(a.length);
}
//length Function
function ArgTest(a, b){
  var i, s = "The ArgTest function expected ";
  var numargs = ArgTest.arguments.length;
  var expargs = ArgTest.length;
  if (expargs < 2)
 s += expargs + " argument. ";
  else
 s += expargs + " arguments. ";
  if (numargs < 2)
 s += numargs + " was passed.";
  else
 s += numargs + " were passed.";
  alert(s);
}
//test prototype
function array_max( ){
  var i, max = this[0];
  for (i = 1; i < this.length; i++)
  {
  if (max < this[i])
  max = this[i];
  }
  return max;
}
Array.prototype.max = array_max;
var x = new Array(1, 2, 3, 4, 5, 6);
var y = x.max( );




</script>
</head>
<body>
<button onclick="result()">
測試引數
</button><br/>
<button onclick="callLevel()">
測試呼叫的函式
</button><br/>
<button onclick="consta()">
測試構造器
</button><br/>
<button onclick="newVar()">
測試顯示函式和隱式函式,以及E的值
</button><br/>
<button onclick="RegExpPropDemo('m')">
測試global
</button><br/>
<button onclick="ownProperty()">
測試Property和ownProperty
</button><br/>
<button onclick="RegExpTest()">
測試index和lastIndex
</button><br/>
<button onclick="inputDemo()">
測試input
</button><br/>
<button onclick="LengthDemo()">
測試length
</button><br/>
<button onclick="ArgTest(1,2)">
測試length of Function
</button><br/>
</body>
<html>