有人可以解释这些javascript概念吗?

时间:2016-03-03 09:25:12

标签: javascript constructor

所以我最近一直在关注this code而且我很难理解一些事情。

当"班级"我首先使用以下代码声明:

<Page x:Class="Marvel_sDatabase.Views.MarvelMenu"
      xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
      xmlns:local="using:Marvel_sDatabase.Views"
      xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
      xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
      xmlns:data="using:Marvel_sDatabase.Models"
      Loaded="Page_Loaded"
      mc:Ignorable="d">

  <Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
    <ProgressRing Name="MyProgressRing"
                  x:FieldModifier="public"
                  Grid.Column="1"
                  Grid.RowSpan="3"
                  Width="50"
                  Height="50"
                  Foreground="{ThemeResource SystemControlBackgroundAccentBrush}"
                  VerticalAlignment="Center"
                  HorizontalAlignment="Center"/>
  </Grid>
</Page>

我说得对function GravityPoint(x, y, radius, targets) { Vector.call(this, x, y); this.radius = radius; this.currentRadius = radius * 0.5; this._targets = { particles: targets.particles || [], gravities: targets.gravities || [] }; this._speed = new Vector(); } 是对的,所以你不必申报位置Vector吗?如果我理解正确,你可以打电话:

Vector.call(this, x, y);

然后var a = new GravityPoint(0,0,10,[..]); ? (假设Vector具有x和y属性)

我的第二个问题是关于这段代码:

a.x == 0

我不理解这种结构:GravityPoint.prototype = (function(o) { var s = new Vector(0, 0), p; for (p in o) s[p] = o[p]; return s; })({ gravity: 0.05, isMouseOver: false, dragging: false, destroyed: false, _easeRadius: 0, _dragDistance: null, _collapsing: false, hitTest: function(p) { return this.distanceTo(p) < this.radius; } ... });

对我而言,它就像将第二部分(MyClass.prototype = (function(o){})({ prop1:val1}))作为参数发送给函数{ prop1:val1},因为这个函数返回一个向量,所以它没有意义。 / p>

感谢您的帮助

1 个答案:

答案 0 :(得分:2)

immediately-invoked function expressions or iffy

简单的例子

(function(a, b) {
  // a == 'hello'
  // b == 'world'
})('hello', 'world');

在你的情况下,你正在定义这个iffy函数

function(o) {
    var s = new Vector(0, 0), p;
    for (p in o) s[p] = o[p];
    return s;
}

然后使用此参数调用它并将结果存储在GravityPoint.prototype中。

{
    gravity:       0.05,
    isMouseOver:   false,
    dragging:      false,
    destroyed:     false,
    _easeRadius:   0,
    _dragDistance: null,
    _collapsing:   false,

    hitTest: function(p) {
        return this.distanceTo(p) < this.radius;
    }
}