Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/entity-framework/4.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
CoffeeScript/LiveScript和数据构造函数_Coffeescript_Livescript - Fatal编程技术网

CoffeeScript/LiveScript和数据构造函数

CoffeeScript/LiveScript和数据构造函数,coffeescript,livescript,Coffeescript,Livescript,我对SML几乎没有经验。现在我想学习LiveScript,但我被数据类型所困扰。那么,可以使用SML/Haskell/OCaml中的数据构造函数来创建类型吗?如果没有,创建数据类型的首选方法是什么?Haskell/SML和JavaScript/LiveScript/CoffeeScript之间的主要区别在于: Haskell/SML是函数式语言,而JS(主要)是命令式语言 Haskell/SML使用严格的静态类型系统,而JS使用动态类型系统 Haskell/SML(大部分)通过设计防止变量的副

我对SML几乎没有经验。现在我想学习LiveScript,但我被数据类型所困扰。那么,可以使用SML/Haskell/OCaml中的数据构造函数来创建类型吗?如果没有,创建数据类型的首选方法是什么?

Haskell/SML和JavaScript/LiveScript/CoffeeScript之间的主要区别在于:

  • Haskell/SML是函数式语言,而JS(主要)是命令式语言
  • Haskell/SML使用严格的静态类型系统,而JS使用动态类型系统
  • Haskell/SML(大部分)通过设计防止变量的副作用。JS没有
在这些函数式语言中,
数据
类型定义用于编译时类型检查——JS的动态类型系统在运行时进行类型检查,因此不需要知道对象的确切结构。因此,
数据
类型定义没有直接翻译

1.仅使用对象 如果您只想在程序中定义一个一次性数据结构,只需实例化一个新对象并赋予它一些属性:

// Create a new object with properties foo and bar
var MyThing = {
    foo: 'a',
    bar: 'b'
};

// Set the baz property of our new object
MyThing.baz = 'c';

// Remove the foo property from our object
delete MyThing.foo;
这在LiveScript中几乎相同,只是语法不太重:

MyThing = { foo: \a, bar: \b }
MyThing.baz = \c
delete MyThing.foo
2.使用JS原型/LiveScript类 如果您要处理一个对象的多个实例,或者任何比定义一个一次性对象更简单的事情,那么您可能需要使用对象原型。在JavaScript中,所有对象都有一个原型对象作为它们的基础。在构造函数上调用
new
操作符时,会得到该函数原型的副本,该副本用作构造函数的
上下文。例如:

// Define our constructor
var Thing = function(foo, bar) {
    this.foo = foo;
    this.bar = bar;
};

// Set a 'default' baz property for all Things
Thing.prototype.baz = 'c';

// Create a Thing
var myThing = new Thing('a', 'b');

// Inspect our properties
console.log(myThing.foo, myThing.bar, myThing.baz) // => 'a', 'b', 'c'
这可以直接在LiveScript中表示:

# Define a constructor
Thing = (foo, bar) ->
    @foo = foo
    @bar = bar

# Set a 'default' baz property for all Things
Thing::baz = \c

# Create a new Thing
my-thing = new Thing \a \b

# Inspect our properties
console.log myThing.foo, myThing.bar, myThing.baz
或者,更简洁地说,使用表示(几乎完全相同)相同内容的
语法:

class Thing
    (@foo, @bar) ->
    baz: \c

my-thing = new Thing \a \b
3.不变的数据结构 如果您来自Haskell或SML,您将熟悉不变性的概念,以及编写不能执行副作用的函数

在最初的示例中,我们声明了对象,
myThing
,然后对其进行了变异。在JS中,对象通过引用传递到函数中,如果不使用调试器,副作用会使我们很难判断出哪里出了问题

为了避免这种情况,我们可以使用,它提供不变的数据结构,如映射(基本上是对象)和列表(基本上是数组)

下面是使用Immutable.js映射重写的原始示例

// Create a new Map (like a standard JS object, but immutable)
var myThing = Immutable.Map({ foo: 'a', bar: 'b' });

/*
 * Set the baz property of our new Map
 * Note that the original `myThing` object remains unchanged,
 * because `myThing.set` returns a new Map with the changes supplied to set
 */
var myThingWithBaz = myThing.set('baz', 'c');

var myThingWithBazButNotFoo = myThingWithBaz.delete('foo');

Haskell/SML和JavaScript/LiveScript/CoffeeScript之间的主要区别在于:

  • Haskell/SML是函数式语言,而JS(主要)是命令式语言
  • Haskell/SML使用严格的静态类型系统,而JS使用动态类型系统
  • Haskell/SML(大部分)通过设计防止变量的副作用。JS没有
在这些函数式语言中,
数据
类型定义用于编译时类型检查——JS的动态类型系统在运行时进行类型检查,因此不需要知道对象的确切结构。因此,
数据
类型定义没有直接翻译

1.仅使用对象 如果您只想在程序中定义一个一次性数据结构,只需实例化一个新对象并赋予它一些属性:

// Create a new object with properties foo and bar
var MyThing = {
    foo: 'a',
    bar: 'b'
};

// Set the baz property of our new object
MyThing.baz = 'c';

// Remove the foo property from our object
delete MyThing.foo;
这在LiveScript中几乎相同,只是语法不太重:

MyThing = { foo: \a, bar: \b }
MyThing.baz = \c
delete MyThing.foo
2.使用JS原型/LiveScript类 如果您要处理一个对象的多个实例,或者任何比定义一个一次性对象更简单的事情,那么您可能需要使用对象原型。在JavaScript中,所有对象都有一个原型对象作为它们的基础。在构造函数上调用
new
操作符时,会得到该函数原型的副本,该副本用作构造函数的
上下文。例如:

// Define our constructor
var Thing = function(foo, bar) {
    this.foo = foo;
    this.bar = bar;
};

// Set a 'default' baz property for all Things
Thing.prototype.baz = 'c';

// Create a Thing
var myThing = new Thing('a', 'b');

// Inspect our properties
console.log(myThing.foo, myThing.bar, myThing.baz) // => 'a', 'b', 'c'
这可以直接在LiveScript中表示:

# Define a constructor
Thing = (foo, bar) ->
    @foo = foo
    @bar = bar

# Set a 'default' baz property for all Things
Thing::baz = \c

# Create a new Thing
my-thing = new Thing \a \b

# Inspect our properties
console.log myThing.foo, myThing.bar, myThing.baz
或者,更简洁地说,使用表示(几乎完全相同)相同内容的
语法:

class Thing
    (@foo, @bar) ->
    baz: \c

my-thing = new Thing \a \b
3.不变的数据结构 如果您来自Haskell或SML,您将熟悉不变性的概念,以及编写不能执行副作用的函数

在最初的示例中,我们声明了对象,
myThing
,然后对其进行了变异。在JS中,对象通过引用传递到函数中,如果不使用调试器,副作用会使我们很难判断出哪里出了问题

为了避免这种情况,我们可以使用,它提供不变的数据结构,如映射(基本上是对象)和列表(基本上是数组)

下面是使用Immutable.js映射重写的原始示例

// Create a new Map (like a standard JS object, but immutable)
var myThing = Immutable.Map({ foo: 'a', bar: 'b' });

/*
 * Set the baz property of our new Map
 * Note that the original `myThing` object remains unchanged,
 * because `myThing.set` returns a new Map with the changes supplied to set
 */
var myThingWithBaz = myThing.set('baz', 'c');

var myThingWithBazButNotFoo = myThingWithBaz.delete('foo');

Haskell/SML和JavaScript/LiveScript/CoffeeScript之间的主要区别在于:

  • Haskell/SML是函数式语言,而JS(主要)是命令式语言
  • Haskell/SML使用严格的静态类型系统,而JS使用动态类型系统
  • Haskell/SML(大部分)通过设计防止变量的副作用。JS没有
在这些函数式语言中,
数据
类型定义用于编译时类型检查——JS的动态类型系统在运行时进行类型检查,因此不需要知道对象的确切结构。因此,
数据
类型定义没有直接翻译

1.仅使用对象 如果您只想在程序中定义一个一次性数据结构,只需实例化一个新对象并赋予它一些属性:

// Create a new object with properties foo and bar
var MyThing = {
    foo: 'a',
    bar: 'b'
};

// Set the baz property of our new object
MyThing.baz = 'c';

// Remove the foo property from our object
delete MyThing.foo;
这在LiveScript中几乎相同,只是语法不太重:

MyThing = { foo: \a, bar: \b }
MyThing.baz = \c
delete MyThing.foo
2.使用JS原型/LiveScript类 如果您要处理一个对象的多个实例,或者任何比定义一个一次性对象更简单的事情,那么您可能需要使用对象原型。在JavaScript中,所有对象都是ha