直到现在我还没有看到这种JavaScript语法,它究竟做了什么? [英] This JavaScript syntax I haven't seen till now, what does it do really?

查看:94
本文介绍了直到现在我还没有看到这种JavaScript语法,它究竟做了什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

今天我看到了一种JavaScript语法(当调用一个函数时),这对我来说并不熟悉。它是这样的:
$ b $ pre $ def $('Person')({
init:function(name){this.name = name;}
,speak:function(text){alert(text ||'我的名字是'+ this.name);}
});

  def('忍者')<< Person({
kick:function(){this.speak('I kick u!');}
});



<1>:第一个例子中括号内的对象会发生什么?它在某种程度上由 def 函数处理,但我不明白这里发生了什么(请参阅 def 功能如下)。对象去哪里?



2:再次大致相同,但使用<< 运营商,我从来没有见过(我想!)。这是什么?



代码来自 http: //gist.github.com/474994 ,其中Joe Dalton已经做出了一个小的JavaScript-OO继承事物(显然,它是别人工作的一个分支,但看起来完全重写)。也许你想在那里查看 def 函数引用的内容,我在这里给你:

 函数def(klassName,上下文){
上下文|| (context = global);

//在给定的上下文中创建类(默认为全局对象)
var Klass =
context [klassName] = function Klass(){

//作为构造函数调用
if(this!= context){

//允许init方法返回不同的类/对象
返回this.init&& amp ; this.init.apply(this,arguments);
}

//作为方法调用
//推迟超类和插件的设置
deferred._super = Klass;
deferred._plugins = arguments [0] || {};
};

//添加静态帮助器方法
Klass.addPlugins = addPlugins;

//当不是
//从一个超类继承时调用函数
deferred = function(plugins){
return Klass.addPlugins(plugins);
};

// valueOf被调用来设置
//从超类继承
deferred.valueOf = function(){
var Superclass = deferred._super;
if(!Superclass)
返回Klass;
Subclass.prototype = Superclass.prototype;
Klass.prototype = new Subclass;
Klass.superclass = Superclass;
Klass.prototype.constructor = Klass;
返回Klass.addPlugins(deferred._plugins);
};
递延递延;


解决方案

c $ c> def('Person')返回一个函数,该函数以对象作为参数调用。这与原理相同:

  function x(){
return function(y){alert(y); }
}

x()('Hello world!');



<2>:< 运算符是左移运算符。它将整数值向特定位数移动到左侧。我还没有找到任何其他用途的参考,并且Javascript中没有操作符重载,所以我无法在函数上使用它。到目前为止,它看起来像一个错字。



编辑:



正如Tim解释的那样,移位运算符是只是用来调用 valueOf 方法。它的工作方式就像所有操作员的重载,接管原来的目的并完成一些完全不同的事情。


Today I saw a JavaScript syntax (when invoking a function) that is unfamiliar to me. It was like:

def('Person') ({
  init: function(name) {this.name=name;}
  ,speak: function(text) {alert(text || 'Hi, my name is ' + this.name);}
});

, and

def('Ninja') << Person ({
  kick: function() {this.speak('I kick u!');}
});

1: What happens with the object within the parentheses in the first example? It is handled by the def function somehow, but I don't understand what is going on here (see the def function below). Where does the object go?

2: About the same thing again, but a use of the << operator that I never seen (I think!). What's that all about?

The code is from http://gist.github.com/474994, where Joe Dalton has made a small JavaScript-OO-inheritance thing (it is apparently a fork of someone else's work, but quite thoroughly rewritten, as it seems). Maybe you want to check it out there for the stuff referenced by the def function, which I give you here:

function def(klassName, context) {
  context || (context = global);

  // Create class on given context (defaults to global object)
  var Klass =
    context[klassName] = function Klass() {

      // Called as a constructor
      if (this != context) {

        // Allow the init method to return a different class/object
        return this.init && this.init.apply(this, arguments);
      }

      // Called as a method
      // defer setup of superclass and plugins
      deferred._super = Klass;
      deferred._plugins = arguments[0] || { };
    };

  // Add static helper method
  Klass.addPlugins = addPlugins;

  // Called as function when not
  // inheriting from a superclass
  deferred = function(plugins) {
    return Klass.addPlugins(plugins);
  };

  // valueOf is called to set up
  // inheritance from a superclass
  deferred.valueOf = function() {
    var Superclass = deferred._super;
    if (!Superclass)
        return Klass;
    Subclass.prototype = Superclass.prototype;
    Klass.prototype = new Subclass;
    Klass.superclass = Superclass;
    Klass.prototype.constructor = Klass;
    return Klass.addPlugins(deferred._plugins);
  };
  return deferred;
}

解决方案

1: The call def('Person') returns a function, which is called with the object as parameter. It's the same principle as:

function x() {
  return function(y) { alert(y); }
}

x()('Hello world!');

2: The << operator is the left shift operator. It shifts an integer value a specific number of bits to the left. I haven't found any reference for any other use for it, and there is no operator overloading in Javascript, so I can't make any sense out of using it on a function. So far it looks like a typo to me.

Edit:

As Tim explained, the shift operator is just used to induce a call to the valueOf method. It works like an overload of all operators, taking over the original purpose and doing something completely different.

这篇关于直到现在我还没有看到这种JavaScript语法,它究竟做了什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆