兰波,你别只顾着外国人呀,CSDN里也需要你呀。anonymous() 创建高级对象
使用构造函数来创建对象
在 Microsoft JScript 中,可以使用构造函数来创建和生成对象类。您可以使用 new 语句来调用一个构造函数。该语句将返回其创建的内容。 
Function 构造函数这个特例可用来创建匿名的函数。所谓匿名函数是指没有名称的函数。例如,可以使用 Function 构造函数来生成一个 “不工作”的函数,作为在另一个函数内的指示。这种函数只会从一个位置调用,不需要名称。 在下例中,这个匿名函数将生成一行“姓名和电子邮件地址”列表。该函数检查 firstNameFirst 变量的值来决定是先输入名还是姓,然后检查 emailNameFirst 变量的值来决定是先输入姓名还是电子邮件地址。该示例假定已在别的位置设置了firstNameFirst 和 emailNameFirst 的值。 for (j = 1; j < addressList[length]; j++) 
{
oneListingLine = new Function(emailNameFirst, firstNameFirst, addressList, j, theName = new Function(firstNameFirst, addressList, j, var theName=(addressList[j].firstName + addressList[j].lastName); 
if(firstNameFirst)
      {
      theName=(addressList[j].firstName + addressList[j].lastName);
      },) ; 
if (emailNameFirst) 
      {
theListing = addressList[j].emailName+ ":\t" + theName 
      } else theListing = theName + ":\t" + addressList[j].emailName; return theListing;)
document.write(oneListingLine + "<br>");
}编写构造函数
要编写自己的构造函数,可以在构造函数中使用 this 关键字来引用新创建的对象。该构造函数将初始化该对象。
尽管在下面的示例中构造函数是从 0 索引开始的,但这并不是必需的。例如,如果希望某个参数指明数组或对象的实际索引数,您可以将 1 作为第一个索引开始。 在该示例中,该参数是 extent 来使其区别于由内置的 Array( ) 对象中自动维护的 length 参数。如果编写了给数组添加属性的代码,必需更新 extent 参数(或您自己的对应参数),因为该参数不是由 JScript 维护的。注意即使是这个极端简单的示例也使用了对象(点)和数组(方括号)的标注风格来引用当前对象。 function MakeStringArray(length) {
this.extent = length;
for (iNum = 0; iNum < length; i++) {
this[iNum] = "";
}
}// 使用该构造函数来创建和初始化一个数组。
myStringArray = new MakeStringArray(63);使用原型来创建对象
在编写对象定义时,可以使用 prototype 属性来创建由该定义生成且所有对象都具有的属性。原型属性将按引用复制给类中的每个对象,因此这一类中的所有对象的这个属性都相同。不过,可以在一个对象中更改原型属性的值,新的值将覆盖默认值,但仅在该实例中有效。属于这个类的其他对象不受此更改的影响。 
使用这个原则,可以给 JScript 语言内部的对象定义附加属性,所有这些对象都具有原型。例如,如果要在计算中使用一个特殊的常数,而在 Math 和 Number 对象中提供的常数没有包括该常数,则可以自己定义,并对其指定各自的对象原型,或对象类的原型属性。 Math.prototype.Avogadro = 6.0232E23;
function howManyMolecules(wtGrams,molWt) {
return ((wtGrams/molWt)*Math.prototype.Avogadro);
}
document.write("There are " + howManyMolecules(window.prompt("How many grams?",0),window.prompt
("What's the molecular weight?",0)) +
 " molecules in that amount.");也许还需要指出的一点是,您可以定义一个函数,将其指定为 String.prototype 的一个方法,并在脚本的任何地方随字符串使用该函数。下面的示例假定已有一个在脚本的其他地方定义的、称为 "theElements" 的 元素周期表(Periodic Chart)数组,该数组中包含了元素的符号、名称、原子数以及其他相关信息。 function atomName(theSymbol) {
return(theElements[theSymbol].fullName);
}String.prototype.atomName = atomName;function decodeFormula(theFormula) {
var theCurrentPiece = "";
var theDecodedFormula = "";
for (i = 1; i = theFormula.length ; i++);
if (theFormtheCurrentPiece 
// 将公式字符串分为符号和数字的数组。
// 循环该公式数组并装配拆开的字符串。每一项为:
theDecodedFormula += formula[n].number
theDecodedFormula += " ";
theDecodedFormula += formula[n].symbol.prototype.atomName;
theDecodedFormula += " "
// 循环结束。return theDecodedFormula;
}decodeFormula(window.prompt("Formula?","Al2O3"));

解决方案 »

  1.   

    This is explain in js1.5:function The function operator defines an anonymous function inside an expression.
      Implemented in  
     JavaScript 1.5  
     Syntax 
    {var | const} variableName = function(parameters) {functionBody}; 
    Description 
    The following examples shows how the function operator is used. This example declares an unnamed function inside an expression. It sets x to a function that returns the square of its argument: var x = function(y) {return y*y}; The next example declares array a as an array of three functions: var a = [function(y) {return y}, function y {return y*y}, function (y) [return y*y*y}]; For this array, a[0](5) returns 5, a[1](5) returns 25, and a[2](5) returns 125. 
      

  2.   

    meizz(梅花雨) 大哥。你的这些我在Jscript的chm文档有,里面压根就没写anonymous()的用法。呵呵。你混得不错呀。都两颗星了。net_lover(孟子E章) 兄的都是E文,我再啃啃
    看看这个例子。很有意思的,可以研究研究:)
    <html>
    <body>
    <button onclick="alert('hha')" id=myButton>button</button>
    <script>
    alert(myButton.onclick);
    </script>
    <button onclick="eval(myButton.onclick+'anonymous()')">run</button>
    </body>
    </html>
      

  3.   

    anonymous()是个 无名 的函数 -- 做事却不留名 
    常用于事件处理中。
    这里有篇文章解释的比较好。
    http://www.brainjar.com/dhtml/events/
      

  4.   

    document.onclick = Function("alert(0);");
    alert(document.onclick);function d() {
        alert(0);
    }
    document.onclick = d;
    alert(document.onclick);上述两个实现的功能其实是一样的,不过第一个用的就是anonymous函数,因为document.onclick并没有像第二个document.onclick明确地指定为d()这个函数,所以称之为anonymous函数。另为从上面的alert()中也可以看到不同。其实其他许多<... onclick="alert(0);">也是anonymous函数。