用eval和call无法理解这个片段

时间:2015-08-14 03:16:53

标签: javascript vbscript jscript

我找到了一个在这两个链接here上混合使用VBScript和JScript的演示,它在code.google.com上提到了完整的代码

这是代码。

我不明白代码,那没关系。我想尝试一下,看看我可以从中得到的结果或者我可以写的类似的东西,然后我可以研究它。它运行没有错误。

(这不是我在标题中引用的代码段,这是代码段的背景。该代码段跟随此内容)

//
// JavaScript unit
// Wrapper for VBScript features in JScript
//
// Copyright (c) 2009 by Ildar Shaimordanov
//
// VERY IMPORTANT!
// InputBox/MsgBox/vbXXX and their descriptions are parts of the CHM-file named as 
// "Windows Script Technologies " and all of them are properties of Microsoft 
// accordingly their copyright as below:
//
// Copyright (c) 2001 Microsoft Corporation. 
// Build: Topic Version 5.6.9309.1546 
//


var vb = {};


/**
 * The helper method creates VBScript functions to be available in JScript.
 *
 * @param       String  The name of VBScript function. 
 * @return      Function
 */
vb.Function = function(func)
{
        return function()
        {
                return vb.Function.eval.call(this, func, arguments);
        };
};

(function()
{

var vbe;

function VBEngine()
{
        if ( vbe ) {
                return;
        }

        vbe = new ActiveXObject('ScriptControl');
        vbe.Language = 'VBScript';
        vbe.AllowUI = true;
};

/**
 * The helper method runs VBScript functions to be available in JScript.
 *
 * @param       String  The name of VBScript function. 
 * @param       Array   The list of arguments for a VBScript function.
 * @return      Function
 */
vb.Function.eval = function(func)
{
        var args = Array.prototype.slice.call(arguments[1]);
        for (var i = 0; i < args.length; i++) {
                if ( typeof args[i] != 'string' ) {
                        continue;
                }
                args[i] = args[i].replace(/["\n\r]/g, function($0)
                {
                        return '" + Chr(' + $0.charCodeAt($0) + ') + "';
                });
                args[i] = '"' + args[i] + '"';
        }

        VBEngine();

        return vbe.eval(func + '(' + args.join(', ') + ')');
};

})();

/**
 * vb.Function.declare()
 *
 * @description
 * Allows to declare custom VBScript functions. 
 * Returns the reference to the htmlfile document. 
 * All declarations are in the namespace of this document. 
 *
 * @example
 * // declare the generator of integer number from L to U
 * var vbs = vb.Function.declare([
 *     'Randomize', 
 *     'Function Random(L, U)', 
 *     '    Random = Int((U - L + 1) * Rnd + L)', 
 *     'End Function']);
 * 
 * // The bones generator
 * var n = vbs.Random(1, 6);
 *
 * @param       Array|String    VBScript declaratiom
 * @return      HTMLDocument
 * @access      static
 */
vb.Function.declare = function()
{
        // Initialize the declarator of VBScript items
        vb.Function.declarator = (new ActiveXObject('htmlfile')).parentWindow;

        // Redeclare the method
        vb.Function.declare = function()
        {
                var declarator = vb.Function.declarator;

                if ( ! arguments.length ) {
                        return declarator;
                }

                var args = arguments;
                if ( args[0] && args[0].constructor == Array ) {
                        args = args[0];
                }
                var func = Array.prototype.slice.call(args).join('\n');

                var document = declarator.document;
                document.write('\x3Cscript language="VBScript"\x3E' + func + '\x3C/script\x3E');

                return declarator;
        };

        // Call new method
        return vb.Function.declare.apply(null, arguments);
};


/**
 * Displays a prompt in a dialog box, waits for the user to input 
 * text or click a button, and returns the contents of the text box. 
 *
 * InputBox(prompt[, title][, default][, xpos][, ypos][, helpfile, context])
 *
 * prompt 
 * String expression displayed as the message in the dialog box. 
 * The maximum length of prompt is approximately 1024 characters, 
 * depending on the width of the characters used. If prompt 
 * consists of more than one line, you can separate the lines using 
 * a carriage return character (Chr(13)), a linefeed character 
 * (Chr(10)), or carriage return-linefeed character combination 
 * (Chr(13) & Chr(10)) between each line. 
 *
 * title 
 * String expression displayed in the title bar of the dialog box. 
 * If you omit title, the application name is placed in the title 
 * bar. 
 *
 * default 
 * String expression displayed in the text box as the default 
 * response if no other input is provided. If you omit default, 
 * the text box is displayed empty. 
 *
 * xpos 
 * Numeric expression that specifies, in twips, the horizontal 
 * distance of the left edge of the dialog box from the left edge 
 * of the screen. If xpos is omitted, the dialog box is 
 * horizontally centered. 
 *
 * ypos 
 * Numeric expression that specifies, in twips, the vertical 
 * distance of the upper edge of the dialog box from the top of the 
 * screen. If ypos is omitted, the dialog box is vertically 
 * positioned approximately one-third of the way down the screen. 
 *
 * helpfile 
 * String expression that identifies the Help file to use to 
 * provide context-sensitive Help for the dialog box. If helpfile is 
 * provided, context must also be provided. 
 *
 * context 
 * Numeric expression that identifies the Help context number 
 * assigned by the Help author to the appropriate Help topic. 
 * If context is provided, helpfile must also be provided. 
 *
 * Remarks
 * When both helpfile and context are supplied, a Help button is 
 * automatically added to the dialog box.
 * If the user clicks OK or presses ENTER, the InputBox function 
 * returns whatever is in the text box. If the user clicks Cancel, 
 * the function returns a zero-length string ("").
 */
var InputBox = vb.Function('InputBox');


/**
 * Displays a message in a dialog box, waits for the user to click 
 * a button, and returns a value indicating which button the user 
 * clicked.
 *
 * MsgBox(prompt[, buttons][, title][, helpfile, context])
 *
 * prompt
 * String expression displayed as the message in the dialog box. 
 * The maximum length of prompt is approximately 1024 characters, 
 * depending on the width of the characters used. If prompt consists 
 * of more than one line, you can separate the lines using a 
 * carriage return character (Chr(13)), a linefeed character 
 * (Chr(10)), or carriage return-linefeed character combination 
 * (Chr(13) & Chr(10)) between each line. 
 *
 * buttons
 * Numeric expression that is the sum of values specifying the 
 * number and type of buttons to display, the icon style to use, 
 * the identity of the default button, and the modality of the 
 * message box. See vb.XXX constants for values. If omitted, 
 * the default value for buttons is 0. 
 *
 * title
 * String expression displayed in the title bar of the dialog box. 
 * If you omit title, the application name is placed in the 
 * title bar. 
 *
 * helpfile
 * String expression that identifies the Help file to use to 
 * provide context-sensitive Help for the dialog box. If helpfile 
 * is provided, context must also be provided. Not available on 
 * 16-bit platforms. 
 *
 * context
 * Numeric expression that identifies the Help context number 
 * assigned by the Help author to the appropriate Help topic. 
 * If context is provided, helpfile must also be provided. 
 * Not available on 16-bit platforms. 
 *
 * Remarks
 * See vb.XXX constants for identify a returned value. 
 * When both helpfile and context are provided, the user can 
 * press F1 to view the Help topic corresponding to the context. 
 * If the dialog box displays a Cancel button, pressing the ESC key 
 * has the same effect as clicking Cancel. If the dialog box 
 * contains a Help button, context-sensitive Help is provided for 
 * the dialog box. However, no value is returned until one of the 
 * other buttons is clicked.
 * When the MsgBox function is used with Microsoft Internet Explorer, 
 * the title of any dialog presented always contains "VBScript:" to 
 * differentiate it from standard system dialogs. 
 */
var MsgBox = vb.Function('MsgBox');


/**
 * The following constants are used with the MsgBox function to 
 * identify what buttons and icons appear on a message box and which 
 * button is the default. In addition, the modality of the MsgBox 
 * can be specified. Since these constants are built into VBScript, 
 * you don't have to define them before using them. Use them 
 * anywhere in your code to represent the values shown for each. 
 */
vb.OKOnly           =    0; // Display OK button only. 
vb.OKCancel         =    1; // Display OK and Cancel buttons. 
vb.AbortRetryIgnore =    2; // Display Abort, Retry, and Ignore buttons. 
vb.YesNoCancel      =    3; // Display Yes, No, and Cancel buttons. 
vb.YesNo            =    4; // Display Yes and No buttons. 
vb.RetryCancel      =    5; // Display Retry and Cancel buttons. 
vb.Critical         =   16; // Display Critical Message icon.  
vb.Question         =   32; // Display Warning Query icon. 
vb.Exclamation      =   48; // Display Warning Message icon. 
vb.Information      =   64; // Display Information Message icon. 
vb.DefaultButton1   =    0; // First button is default. 
vb.DefaultButton2   =  256; // Second button is default. 
vb.DefaultButton3   =  512; // Third button is default. 
vb.DefaultButton4   =  768; // Fourth button is default. 
vb.ApplicationModal =    0; // Application modal; the user must respond to the message box before continuing work in the current application. 
vb.SystemModal      = 4096; // System modal; all applications are suspended until the user responds to the message box. 


/**
 * The following constants are used with the MsgBox function to 
 * identify which button a user has selected. These constants are 
 * only available when your project has an explicit reference to 
 * the appropriate type library containing these constant 
 * definitions. For VBScript, you must explicitly declare 
 * these constants in your code. 
 */
vb.OK     = 1; // OK 
vb.Cancel = 2; // Cancel 
vb.Abort  = 3; // Abort 
vb.Retry  = 4; // Retry 
vb.Ignore = 5; // Ignore 
vb.Yes    = 6; // Yes 
vb.No     = 7; // No 

这是我正在尝试的以下代码段的一些背景知识。

所以我尝试编写以下小片段并编译/解释很好。没有错误

a={};
a.abc = function(func)
{
        return function()
        {
                return a.abc.eval.call(this, func, arguments);
        };
};

a.abc("asdf");

它运行没有错误。

我理解a在那里被定义为一个对象,我们正在为它添加一个函数,并且该函数返回一个函数。虽然这个功能做了什么,但我不确定。

试图理解它......看起来eval是一个函数,因为在它之后调用了调用,如果eval是一个属性,则无法完成。

(我知道我可以自己做eval,例如从控制台eval(“5 + 6”);所以eval有一个函数)

但是如果我把那个片段放在控制台中(以便创建一个对象及其abc函数),那么我就

typeof a.abc.eval&lt; - undefined

a.abc.eval()&lt; - “未捕获的TypeError:a.abc.eval不是函数”

我不明白为什么它说undefined并且当我的代码显示“a.abc.eval.call”时a.abc.eval不是函数

0 个答案:

没有答案