小编Ban*_*San的帖子

什么是(功能)反应式编程?

我已经读过关于反应式编程的维基百科文章.我还阅读了关于功能反应式编程的小文章.描述非常抽象.

  1. 功能反应式编程(FRP)在实践中意味着什么?
  2. 反应式编程(与非反应式编程相反)是由什么组成的?

我的背景是命令式/ OO语言,因此可以理解与此范例相关的解释.

functional-programming terminology frp reactive-programming

1149
推荐指数
15
解决办法
25万
查看次数

一个AngularJS控制器可以调用另一个吗?

是否有可能让一个控制器使用另一个?

例如:

此HTML文档只是打印MessageCtrl控制器在messageCtrl.js文件中传递的消息.

<html xmlns:ng="http://angularjs.org/">
<head>
    <meta charset="utf-8" />
    <title>Inter Controller Communication</title>
</head>
<body>
    <div ng:controller="MessageCtrl">
        <p>{{message}}</p>
    </div>

    <!-- Angular Scripts -->
    <script src="http://code.angularjs.org/angular-0.9.19.js" ng:autobind></script>
    <script src="js/messageCtrl.js" type="text/javascript"></script>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)

控制器文件包含以下代码:

function MessageCtrl()
{
    this.message = function() { 
        return "The current date is: " + new Date().toString(); 
    };
}
Run Code Online (Sandbox Code Playgroud)

这只是打印当前日期;

如果我要添加另一个控制器,DateCtrl它将日期以特定格式交还给MessageCtrl,那怎么会这样做呢?DI框架似乎关注XmlHttpRequests和访问服务.

html javascript angularjs

575
推荐指数
8
解决办法
39万
查看次数

理解Object.create()和new SomeFunction()之间的区别

我最近Object.create()在JavaScript中偶然发现了这个方法,并试图推断它与创建一个对象的新实例有什么不同new SomeFunction(),当你想要使用另一个时.

请考虑以下示例:

var test = {
  val: 1,
  func: function() {
    return this.val;
  }
};
var testA = Object.create(test);

testA.val = 2;
console.log(test.func()); // 1
console.log(testA.func()); // 2

console.log('other test');
var otherTest = function() {
  this.val = 1;
  this.func = function() {
    return this.val;
  };
};

var otherTestA = new otherTest();
var otherTestB = new otherTest();
otherTestB.val = 2;
console.log(otherTestA.val); // 1 
console.log(otherTestB.val); // 2

console.log(otherTestA.func()); // 1
console.log(otherTestB.func()); // 2
Run Code Online (Sandbox Code Playgroud)

请注意,在两种情况下都观察到相同的行为.在我看来,这两种情况之间的主要区别是:

  • Object.create()实际使用的对象实际上形成了新对象的原型,而在new …

javascript prototype object-create

372
推荐指数
10
解决办法
12万
查看次数

无法从程序集'mscorlib加载类型'System.Runtime.CompilerServices.ExtensionAttribute'

当我第一次启动我的网站时,我收到了这个错误

Could not load type 'System.Runtime.CompilerServices.ExtensionAttribute' from assembly 'mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'

我究竟做错了什么?

我正在使用.NET 4并从Visual Studio启动该站点.

我最近唯一改变的是将Simple Injector(通过Nuget)添加到我的项目中.

这是堆栈跟踪

[TypeLoadException: Could not load type 'System.Runtime.CompilerServices.ExtensionAttribute' from assembly 'mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'.]
   System.ModuleHandle.ResolveType(RuntimeModule module, Int32 typeToken, IntPtr* typeInstArgs, Int32 typeInstCount, IntPtr* methodInstArgs, Int32 methodInstCount, ObjectHandleOnStack type) +0
   System.ModuleHandle.ResolveTypeHandleInternal(RuntimeModule module, Int32 typeToken, RuntimeTypeHandle[] typeInstantiationContext, RuntimeTypeHandle[] methodInstantiationContext) +180
   System.Reflection.RuntimeModule.ResolveType(Int32 metadataToken, Type[] genericTypeArguments, Type[] genericMethodArguments) +192
   System.Reflection.CustomAttribute.FilterCustomAttributeRecord(CustomAttributeRecord caRecord, MetadataImport scope, Assembly& lastAptcaOkAssembly, RuntimeModule decoratedModule, MetadataToken decoratedToken, RuntimeType attributeFilterType, Boolean mustBeInheritable, Object[] attributes, IList derivedAttributes, …
Run Code Online (Sandbox Code Playgroud)

.net

142
推荐指数
4
解决办法
16万
查看次数

将模拟注入AngularJS服务

我写了一个AngularJS服务,我想对它进行单元测试.

angular.module('myServiceProvider', ['fooServiceProvider', 'barServiceProvider']).
    factory('myService', function ($http, fooService, barService) {

    this.something = function() {
        // Do something with the injected services
    };

    return this;
});
Run Code Online (Sandbox Code Playgroud)

我的app.js文件已注册:

angular
.module('myApp', ['fooServiceProvider','barServiceProvider','myServiceProvider']
)
Run Code Online (Sandbox Code Playgroud)

我可以测试DI是这样工作的:

describe("Using the DI framework", function() {
    beforeEach(module('fooServiceProvider'));
    beforeEach(module('barServiceProvider'));
    beforeEach(module('myServiceProvder'));

    var service;

    beforeEach(inject(function(fooService, barService, myService) {
        service=myService;
    }));

    it("can be instantiated", function() {
        expect(service).not.toBeNull();
    });
});
Run Code Online (Sandbox Code Playgroud)

这证明了服务可以由DI框架创建,但是接下来我想对服务进行单元测试,这意味着模拟注入的对象.

我该怎么做呢?

我已经尝试将模拟对象放在模块中,例如

beforeEach(module(mockNavigationService));
Run Code Online (Sandbox Code Playgroud)

并将服务定义重写为:

function MyService(http, fooService, barService) {
    this.somthing = function() {
        // Do something with the injected services
    };
});

angular.module('myServiceProvider', …
Run Code Online (Sandbox Code Playgroud)

javascript mocking jasmine angularjs angularjs-service

114
推荐指数
1
解决办法
10万
查看次数

为什么我们需要ng-click?

Angular应用程序使用ng-click()属性而不是onclick事件.

为什么是这样?

javascript web-applications angularjs

73
推荐指数
2
解决办法
4万
查看次数

写入然后从MemoryStream中读取

我正在使用DataContractJsonSerializer,它喜欢输出到Stream.我想要对串行器的输出进行顶部和尾部处理,因此我使用StreamWriter交替写入我需要的额外位.

var ser = new DataContractJsonSerializer(typeof (TValue));

using (var stream = new MemoryStream())
{   
    using (var sw = new StreamWriter(stream))
    {
        sw.Write("{");

        foreach (var kvp in keysAndValues)
        {
            sw.Write("'{0}':", kvp.Key);
            ser.WriteObject(stream, kvp.Value);
        }

        sw.Write("}");
    }

    using (var streamReader = new StreamReader(stream))
    {
        return streamReader.ReadToEnd();
    }
}
Run Code Online (Sandbox Code Playgroud)

当我这样做时,我得到一个ArgumentException"流不可读".

我可能在这里做错了所以所有答案都欢迎.谢谢.

c#

60
推荐指数
3
解决办法
8万
查看次数

如何打印除当前分支以外的分支的日志?

我在一个分支上有一些变化.更改分支是一种痛苦,因为某些文件被进程锁定,因此要更改分支,我必须停止所有具有锁定的进程,然后stash在签出其他分支以查看其日志之前进行更改.

是否可以查看不同分支的日志,而无需查看它?

git branch git-stash git-checkout git-log

53
推荐指数
1
解决办法
2万
查看次数

性能差异......如此戏剧化?

刚才我读了一些关于List<T>vs的帖子LinkedList<T>,所以我决定自己对一些结构进行基准测试.我为基准Stack<T>,Queue<T>,List<T>LinkedList<T>通过从前/结束添加数据和删除数据到/.这是基准测试结果:

               Pushing to Stack...  Time used:      7067 ticks
              Poping from Stack...  Time used:      2508 ticks

               Enqueue to Queue...  Time used:      7509 ticks
             Dequeue from Queue...  Time used:      2973 ticks

    Insert to List at the front...  Time used:   5211897 ticks
RemoveAt from List at the front...  Time used:   5198380 ticks

         Add to List at the end...  Time used:      5691 ticks
  RemoveAt from List at the end...  Time used: …
Run Code Online (Sandbox Code Playgroud)

.net c# collections data-structures

51
推荐指数
3
解决办法
3万
查看次数

异步总是WaitingForActivation

我试图找出async&await关键字的全部内容,但输出并不是我所期待的.

控制台应用程序如下:

class Program
{
    static void Main(string[] args)
    {
        Console.WriteLine("Foo called");
        var result = Foo(5);

        while (result.Status != TaskStatus.RanToCompletion)
        {
            Console.WriteLine("Thread ID: {0}, Status: {1}", Thread.CurrentThread.ManagedThreadId, result.Status);
            Task.Delay(100).Wait();
        }

        Console.WriteLine("Result: {0}", result.Result);
        Console.WriteLine("Finished.");
        Console.ReadKey(true);
    }

    private static async Task<string> Foo(int seconds)
    {
        return await Task.Run(() =>
            {
                for (int i = 0; i < seconds; i++)
                {
                    Console.WriteLine("Thread ID: {0}, second {1}.", Thread.CurrentThread.ManagedThreadId, i);
                    Task.Delay(TimeSpan.FromSeconds(1)).Wait();
                }

                return "Foo Completed.";
            });
    }
}
Run Code Online (Sandbox Code Playgroud)

输出是:

Foo called …
Run Code Online (Sandbox Code Playgroud)

.net c# asynchronous async-await

50
推荐指数
2
解决办法
8万
查看次数