处理对象参数

发布于 2024-09-18 05:07:38 字数 2026 浏览 4 评论 0原文

我是 JavaScript OO 编程新手,如有任何歧义,请耐心等待。

我有一个方法,需要一个对象作为其参数。 所需的对象设置要使用的方法的选项。

var objInstance = csvObj.getData({
    url: '', // csv url

    format: '', // return data format

    onComplete: function (output) {
           // a function that will display data returned
    }
});

csvObj 定义如下

var csvObj = { 
    getData: function (o) {

        var format = o.format || 'json',  //optional options 
            url = o.url, callback = o.onComplete;   // required options

        function include_js(jsurl) {  // private function for performing cross dormain requests
            var script = document.createElement("script");
            script.type = "text/javascript";

            script.src = jsurl;
            document.getElementsByTagName("head")[0].appendChild(script);
        }


        (function () { // anonymous function for proccessing and returning data

            if (typeof  callback === 'function' && url) {  // if require options are defined

                if (!window.call_back) {    // and global call_back function is not yet defined
                    window.call_back = callback;  // define global call_back function (will be called when external script is loaded)
                }

                var restQuery = 'http://query.yahooapis.com/v1/public/yql?q=',
                query = 'select * from csv where url="' +  url + '" and columns="' + cols + '"', 

                yqlQuery = restQuery + encodeURIComponent(query)  + '&format=' + data + '&callback=call_back'; // create YQL query
                include_js(yqlQuery);  // create a cross dormain YQL request 
            } 
        else {
               alert('onComplete function or csv url not defined.'); // required options not defined
            }
        }());  

    }
};

问题:是否有比简单访问更好的方法来处理对象参数:

var privateProperty = object.property;

var privateMethod = object.method;

I am new to JavaScript OO programming please bear with me for any ambiguities.

I have a method that requires an object as its arguments.
The required object sets options for the method to work with.

var objInstance = csvObj.getData({
    url: '', // csv url

    format: '', // return data format

    onComplete: function (output) {
           // a function that will display data returned
    }
});

csvObj is defined below

var csvObj = { 
    getData: function (o) {

        var format = o.format || 'json',  //optional options 
            url = o.url, callback = o.onComplete;   // required options

        function include_js(jsurl) {  // private function for performing cross dormain requests
            var script = document.createElement("script");
            script.type = "text/javascript";

            script.src = jsurl;
            document.getElementsByTagName("head")[0].appendChild(script);
        }


        (function () { // anonymous function for proccessing and returning data

            if (typeof  callback === 'function' && url) {  // if require options are defined

                if (!window.call_back) {    // and global call_back function is not yet defined
                    window.call_back = callback;  // define global call_back function (will be called when external script is loaded)
                }

                var restQuery = 'http://query.yahooapis.com/v1/public/yql?q=',
                query = 'select * from csv where url="' +  url + '" and columns="' + cols + '"', 

                yqlQuery = restQuery + encodeURIComponent(query)  + '&format=' + data + '&callback=call_back'; // create YQL query
                include_js(yqlQuery);  // create a cross dormain YQL request 
            } 
        else {
               alert('onComplete function or csv url not defined.'); // required options not defined
            }
        }());  

    }
};

Question: Is there a better way of handling object arguments than simply accessing like this:

var privateProperty = object.property; or

var privateMethod = object.method;

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

逆蝶 2024-09-25 05:07:38

你的方法是完全有效的。您也可以这样做:

var privateMethod = object['method'];

这对于动态获取属性非常方便,但总的来说我更喜欢 object.method,因为它更容易阅读。

Your approach is totally valid. You could do it like this, too:

var privateMethod = object['method'];

This can be very handy for dynamically getting properties, but in general i prefer object.method, because it is much easier to read.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文