Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/88.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 创建jquery函数_Javascript_Jquery_Function - Fatal编程技术网

Javascript 创建jquery函数

Javascript 创建jquery函数,javascript,jquery,function,Javascript,Jquery,Function,我想知道如何创建jquery函数,然后使用其中声明的参数名称和值调用该函数 而不是以正确的顺序给出变量,以实现如下功能: myFunction("My function name",true,false); 我想给函数一些类似的东西: function({ displayName: "My function name", editable: true, displayForm: false; }) 参数的顺序无关紧要,因为我用它们的名字来命名 我在每个jquery库(jqPlot

我想知道如何创建jquery函数,然后使用其中声明的参数名称和值调用该函数

而不是以正确的顺序给出变量,以实现如下功能:

myFunction("My function name",true,false);
我想给函数一些类似的东西:

function({
  displayName: "My function name",
  editable: true,
  displayForm: false;
})
参数的顺序无关紧要,因为我用它们的名字来命名

我在每个jquery库(jqPlot、JqGrid、noty等)中都看到了这种配置 下面是一个例子:

$(document).ready(function(){
  var plot1 = $.jqplot('chart1', [cosPoints], {  
      series:[{showMarker:false}],
      axes:{
        xaxis:{
          label:'Angle (radians)'
        },
        yaxis:{
          label:'Cosine'
        }
      }
  });
});
--编辑--


我有自己的js函数来打印json中的树,但我有太多的参数,所以很难读取和编辑。

只需声明函数,并将对象作为参数:

function myFunction(obj) {
    if(obj.displayName) {
    // do something with obj.displayName
    }

    // ....
}
您可以按照指示将其命名为:

myFunction({
    displayName: "My function name",
    editable: true,
    displayForm: false;
});
即使这样:

myFunction({
    editable: true,
    displayForm: false;
});
为了测试作为参数传递的对象中是否存在属性,只需像我所做的那样进行测试:
obj.myProperty

另一点是,您可以将默认值与:


只需声明以对象作为参数的函数:

function myFunction(obj) {
    if(obj.displayName) {
    // do something with obj.displayName
    }

    // ....
}
您可以按照指示将其命名为:

myFunction({
    displayName: "My function name",
    editable: true,
    displayForm: false;
});
即使这样:

myFunction({
    editable: true,
    displayForm: false;
});
为了测试作为参数传递的对象中是否存在属性,只需像我所做的那样进行测试:
obj.myProperty

另一点是,您可以将默认值与:


没有理由不按要求执行操作,只需让函数预期对象文字:

function MyFunction(o){
    // Do whatever you need to do with your object properties,
    // like set some corresponding scoped variables.
    // 0 would be their default values, change as appropriate.
    var displayName = o.displayName || 0,
        editable = o.editable || 0,
        displayForm = o.displayForm || 0;
}
那就按你的意思说吧:

myFunction({
    displayName: "My function name",
    editable: true,
    displayForm: false;
});
作为记录,这里没有jQuery功能。这都是普通的JavaScript



注意,如果您使用的是jQuery,那么在继续执行功能之前,您可以检查该参数是否实际上是一个对象文字。

没有理由不能执行您要求的操作,只要让您的函数预期一个对象文字:

function MyFunction(o){
    // Do whatever you need to do with your object properties,
    // like set some corresponding scoped variables.
    // 0 would be their default values, change as appropriate.
    var displayName = o.displayName || 0,
        editable = o.editable || 0,
        displayForm = o.displayForm || 0;
}
那就按你的意思说吧:

myFunction({
    displayName: "My function name",
    editable: true,
    displayForm: false;
});
作为记录,这里没有jQuery功能。这都是普通的JavaScript


注意,如果您使用的是jQuery,那么在继续执行功能之前,您可以检查该参数是否实际上是一个对象文本,扩展名为。

jQuery

jQuery.fn.extend({
    zigzag: function () {
        var text = $(this).text();
        var zigzagText = '';
        var toggle = true; //lower/uppper toggle
            $.each(text, function(i, nome) {
                zigzagText += (toggle) ? nome.toUpperCase() : nome.toLowerCase();
                toggle = (toggle) ? false : true;
            });
    return zigzagText;
    }
});

console.log($('#tagline').zigzag());
//output: #1 jQuErY BlOg fOr yOuR DaIlY NeWs, PlUgInS, tUtS/TiPs & cOdE SnIpPeTs.

//chained example
console.log($('#tagline').zigzag().toLowerCase());
//output: #1 jquery blog for your daily news, plugins, tuts/tips & code snippets.
有关更多参考信息,请查看:

jQuery扩展

jQuery.fn.extend({
    zigzag: function () {
        var text = $(this).text();
        var zigzagText = '';
        var toggle = true; //lower/uppper toggle
            $.each(text, function(i, nome) {
                zigzagText += (toggle) ? nome.toUpperCase() : nome.toLowerCase();
                toggle = (toggle) ? false : true;
            });
    return zigzagText;
    }
});

console.log($('#tagline').zigzag());
//output: #1 jQuErY BlOg fOr yOuR DaIlY NeWs, PlUgInS, tUtS/TiPs & cOdE SnIpPeTs.

//chained example
console.log($('#tagline').zigzag().toLowerCase());
//output: #1 jquery blog for your daily news, plugins, tuts/tips & code snippets.

有关更多参考信息,您可以查看:

The
{}
只是对象的简写

就拿这个例子来说

var person = {firstName:"John", lastName:"Doe", age:46};
如果将其作为未命名对象传递给函数,则可以从中选择值。例如:

function myFunc( myArg ){
    alert( myArg.firstName );
}
并称之为

myFunc({firstName:"John", lastName:"Doe", age:46});

{}
只是对象的简写

就拿这个例子来说

var person = {firstName:"John", lastName:"Doe", age:46};
如果将其作为未命名对象传递给函数,则可以从中选择值。例如:

function myFunc( myArg ){
    alert( myArg.firstName );
}
并称之为

myFunc({firstName:"John", lastName:"Doe", age:46});

如果您只是将对象作为参数传递给函数,那么它工作正常

test({a:"TestA", b:"TestB"});

function test(object)
{
    alert(object.a);
    alert(object.b);
}

如果您只是将对象作为参数传递给函数,那么它工作正常

test({a:"TestA", b:"TestB"});

function test(object)
{
    alert(object.a);
    alert(object.b);
}

是否要创建函数或jquery扩展?只需将对象作为参数传递并在函数中使用即可。是否正在尝试创建自己的jquery插件?是否看到:是否要创建函数或jquery扩展?只需将对象作为参数传递并在函数中使用即可。是否正在尝试创建自己的jquery插件jQuery插件?你看到了吗:比我快。最固溶体。除了可能与预期数据类型同步的默认值之外。@Jonast92当然可以,但在这种情况下,我不会继续假设数据类型。不过我会加一张便条,谢谢。回答得很好。我会检查的。谢谢:我的作品很有魅力。谢谢,谢谢你。最固溶体。除了可能与预期数据类型同步的默认值之外。@Jonast92当然可以,但在这种情况下,我不会继续假设数据类型。不过我会加一张便条,谢谢。回答得很好。我会检查的。谢谢:我的作品很有魅力。谢谢