javascript:作为对象或函数传递

时间:2022-03-25 09:22:23

My question is rather weird, it has to do with something i have seen in jQuery but so far i have been unable to recreate it.

我的问题相当奇怪,它与我在jQuery中看到的一些东西有关,但到目前为止我还无法重新创建它。

in jQuery you can go like this

在jQuery中可以这样操作

jQuery('div').append

or

jQuery.ajax

the application i am making would need a similar syntax, i notice if you use new like

我正在开发的应用程序需要类似的语法,我注意到如果您使用新的like

var that=new function(){
}

you can call the function with just that, without the (), but in some cases i would need it.

你可以用它来调用函数,但在某些情况下,我需要它。

The reason for this is some functions i have need to select a dom element just like jQuery so.

原因是我需要像jQuery一样选择dom元素的一些函数。

that('[data-something="this"]').setEvent('click',functin(){})

and some automatically do it so:

有些人会自动这么做:

that.loadIt('this','[data-something="that"]') 

the reason for this is that the dom elements are loaded externally and pushed, then the script waits for it to be ready before continuing. and doing it this way, to me anyway seems like the most cleanest way to get this functionality (i am coding a full javascript framework so i avoid libraries to keep the scripts fast)

这样做的原因是dom元素被外部加载并推入,然后脚本在继续之前等待它准备好。这样做,对我来说似乎是获得这个功能最干净的方式(我正在编写一个完整的javascript框架,所以我避免使用库来保持脚本的速度)

3 个解决方案

#1


5  

Functions are objects.

函数对象。

Just get rid of new, and add properties directly to that.

去掉新的,直接添加属性。

var that = function() {
    // do some work
}

that.loadit = function() {
    // do other work
}

Since you're trying to achieve something like jQuery does, then have that call a constructor.

既然您想要实现像jQuery这样的功能,那么请调用构造函数。

;(function(global) {

       // function to be publicly exposed
    var that = function(foo, bar) {
        return new MyLibrary(foo, bar);
    }

       // publicly expose the function
    global.that = that;

       // use the function as a namespace for utilities
    that.loadit = function() {
        // do other work
    }

       // The actual constructor function, like the internal jQuery constructor
    MyLibrary(foo, bar) {
        // constructor function
    }

       // Prototypal inheritance of objects created from the constructor
    MyLibrary.prototype.setEvent = function() {
        // do some work
        return this;  // allows for method chaining
    };
    MyLibrary.prototype.otherMethod = function() {
        // do something else
        return this;  // allows for method chaining
    };
})(this);

#2


5  

Functions are objects and can have properties, just like other objects can. So, you can add a property to a function like this:

函数是对象,可以像其他对象一样具有属性。你可以向这样的函数中添加一个属性

function myFunc(){}
myFunc.someFunc = function(){}

If you use new myFunc the resulting object won't have someFunc as it's not part of the prototype.

如果您使用新的myFunc,结果对象将不会有一些func,因为它不是原型的一部分。

So, you can make something like this:

你可以这样做:

function myFunc(){
    // This lets you do "myFunc()" instead of "new myFunc()"
    if (!(this instanceof myFunc)) {
        return new myFunc();
    }
    else{
        this.val = 0;

        this.setVal = function(x){
            this.val = x;
            // for function chaining
            return this;
        }

        this.getVal = function(){
            return this.val;
        }
    }
}

// This function is not part of the prototype
myFunc.test = function(){
    alert('hi');
}

// Some tests
var obj = myFunc();
obj.setVal(12).getVal(); // 12

myFunc.test();

obj.test(); // Error: 'test' is not a function

myFunc.getVal(); // Error: 'getVal' is not a function

#3


0  

$.fn.loadIt=function(var1,var2) {
  // $(this) is automatically passed
  // do stuff
}

call it like this

这样叫它

$('#element').loadIt('a variable','another variable');

#1


5  

Functions are objects.

函数对象。

Just get rid of new, and add properties directly to that.

去掉新的,直接添加属性。

var that = function() {
    // do some work
}

that.loadit = function() {
    // do other work
}

Since you're trying to achieve something like jQuery does, then have that call a constructor.

既然您想要实现像jQuery这样的功能,那么请调用构造函数。

;(function(global) {

       // function to be publicly exposed
    var that = function(foo, bar) {
        return new MyLibrary(foo, bar);
    }

       // publicly expose the function
    global.that = that;

       // use the function as a namespace for utilities
    that.loadit = function() {
        // do other work
    }

       // The actual constructor function, like the internal jQuery constructor
    MyLibrary(foo, bar) {
        // constructor function
    }

       // Prototypal inheritance of objects created from the constructor
    MyLibrary.prototype.setEvent = function() {
        // do some work
        return this;  // allows for method chaining
    };
    MyLibrary.prototype.otherMethod = function() {
        // do something else
        return this;  // allows for method chaining
    };
})(this);

#2


5  

Functions are objects and can have properties, just like other objects can. So, you can add a property to a function like this:

函数是对象,可以像其他对象一样具有属性。你可以向这样的函数中添加一个属性

function myFunc(){}
myFunc.someFunc = function(){}

If you use new myFunc the resulting object won't have someFunc as it's not part of the prototype.

如果您使用新的myFunc,结果对象将不会有一些func,因为它不是原型的一部分。

So, you can make something like this:

你可以这样做:

function myFunc(){
    // This lets you do "myFunc()" instead of "new myFunc()"
    if (!(this instanceof myFunc)) {
        return new myFunc();
    }
    else{
        this.val = 0;

        this.setVal = function(x){
            this.val = x;
            // for function chaining
            return this;
        }

        this.getVal = function(){
            return this.val;
        }
    }
}

// This function is not part of the prototype
myFunc.test = function(){
    alert('hi');
}

// Some tests
var obj = myFunc();
obj.setVal(12).getVal(); // 12

myFunc.test();

obj.test(); // Error: 'test' is not a function

myFunc.getVal(); // Error: 'getVal' is not a function

#3


0  

$.fn.loadIt=function(var1,var2) {
  // $(this) is automatically passed
  // do stuff
}

call it like this

这样叫它

$('#element').loadIt('a variable','another variable');