Is there a way to get line number and function name in Swift language?


The call() magic function is to class functions what __get() is to class variables – if you call meow() on an object of class dog, PHP will fail to find the function and check whether you have defined a __call() function. If so, your __call() is used, with the name of the function you tried to call and the parameters you passed being passed in as parameters one and two respectively.

Here’s an example of __call() in action:

class dog {
        public $Name;

        public function bark() {
            print "Woof!n";

        // public function meow() {
            // print "Dogs don't meow!n";
        // }

        public function __call($function, $args) {
            $args = implode(', ', $args);
            print "Call to $function() with args '$args' failed!n";

    $poppy = new dog;
    $poppy->meow("foo", "bar", "baz");

Again, note that the meow() function is commented out – if you want to be sure that __call() is not used if the function already exists, remove the comments from meow().

에서 참조

It’s usually to namespace (see later) and control the visibility of member functions and/or variables. Think of it like an object definition. jQuery plugins are usually written like this.

In Javascript, you can nest functions. So, the following is legal:

function outerFunction() { function innerFunction() { // code } }

Now you can call outerFunction(), but the visiblity of innerFunction() is limited to the scope of outerFunction(), meaning it is private to outerFunction(). It basically follows the same principle as variables in Javascript:

var globalVariable; function someFunction() { var localVariable; }


function globalFunction() { var localFunction1 = function() { //I'm anonymous! But localFunction1 is a reference to me! }; function localFunction2() { //I'm named! } }

In the above scenario, you can call globalFunction() from anywhere, but you cannot call localFunction1 or localFunction2.

What you’re doing when you write (function() { ... code ... })(), is you’re making the code inside a function literal (meaning the whole “object” is actually a function). After that, you’re self-invoking the function (the final ()). So the major advantage of this as I mentioned before, is that you can have private methods/functions and properties:

(function() { var private_var; function private_function() { //code } })()

The neat thing is that you can also define things inside and expose it to the outside world so (an example of namespacing so you can basically create your own library/plugin):

var myPlugin = (function() { var private_var; function private_function() { } return { public_function1: function() { }, public_function2: function() { } } })()

Now you can call myPlugin.public_function1(), but you cannot access private_function()! So pretty similar to a class definition. To understand this better, I recommend the following links for some further reading:


I forgot to mention. In that final (), you can pass anything you want inside. For example, when you create jQuery plugins, you pass in jQuery or $ like so:

(function(jQ) { ... code ... })(jQuery) 

So what you’re doing here is defining a function that takes in one parameter (called jQ, a local variable, and known only to that function). Then you’re self-invoking the function and passing in a parameter (also called jQuery, but this one is from the outside world and a reference to the actual jQuery itself). This is because everything you define inside is effectively inside the function. So if you need to know about anything from the outside world, you need to pass it in