顯示具有 Node.js、Pomelo 標籤的文章。 顯示所有文章
顯示具有 Node.js、Pomelo 標籤的文章。 顯示所有文章

2015/04/15

在.net framework 4.5程式中,使用node.js

Edge.js

1. 準備好 Visual Studio 2010 / 2012

2. 安裝Visual Studio 2010 / 2012 擴充套件:NuGet 套件管理員
https://visualstudiogallery.msdn.microsoft.com/27077b70-9dad-4c64-adcf-c7cf6bc9970c

3. 開啟 Visual Studio,新建一個 .net framework 4.5的C#主控台應用程式

4. 在專案上,按滑鼠右鍵,選擇 "管理NuGet套件",加入並按裝Edge,js

5. 加入以下測試程式
using System;
using System.Threading.Tasks;
using EdgeJs;

class Program
{
    public static async void Start()
    {
        var func = Edge.Func(@"
            return function (data, callback) {
                callback(null, 'Node.js welcomes ' + data);
            }
        ");

        Console.WriteLine(await func(".NET"));
    }

    static void Main(string[] args)
    {
        Task.Run((Action)Start).Wait();
    }
}


Edge.js:
https://github.com/tjanczuk/edge/tree/master#what-you-need-1

NuGet:
https://www.nuget.org/packages/Edge.js

2015/03/04

Javascript的this用法

最近在學習Javascript,當然是用在各方面;但是關於Javascript的this,不同於C++而言,困擾了一下!在網路上找到這篇文章,轉貼一下,很有幫助喔 ^_^
作者: 阮一峰
日期: 2010年4月30日
this是Javascript语言的一个关键字。
它代表函数运行时,自动生成的一个内部对象,只能在函数内部使用。比如,
  function test(){
    this.x = 1;
  }
随着函数使用场合的不同,this的值会发生变化。但是有一个总的原则,那就是this指的是,调用函数的那个对象。
下面分四种情况,详细讨论this的用法。
情况一:纯粹的函数调用
这是函数的最通常用法,属于全局性调用,因此this就代表全局对象Global。
请看下面这段代码,它的运行结果是1。
  function test(){
    this.x = 1;
    alert(this.x);
  }
  test(); // 1
为了证明this就是全局对象,我对代码做一些改变:
  var x = 1;
  function test(){
    alert(this.x);
  }
  test(); // 1
运行结果还是1。再变一下:
  var x = 1;
  function test(){
    this.x = 0;
  }
  test();
  alert(x); //0
情况二:作为对象方法的调用
函数还可以作为某个对象的方法调用,这时this就指这个上级对象。
  function test(){
    alert(this.x);
  }
  var o = {};
  o.x = 1;
  o.m = test;
  o.m(); // 1
情况三 作为构造函数调用
所谓构造函数,就是通过这个函数生成一个新对象(object)。这时,this就指这个新对象。
  function test(){
    this.x = 1;
  }
  var o = new test();
  alert(o.x); // 1
运行结果为1。为了表明这时this不是全局对象,我对代码做一些改变:
  var x = 2;
  function test(){
    this.x = 1;
  }
  var o = new test();
  alert(x); //2
运行结果为2,表明全局变量x的值根本没变。
情况四 apply调用
apply()是函数对象的一个方法,它的作用是改变函数的调用对象,它的第一个参数就表示改变后的调用这个函数的对象。因此,this指的就是这第一个参数。
  var x = 0;
  function test(){
    alert(this.x);
  }
  var o={};
  o.x = 1;
  o.m = test;
  o.m.apply(); //0
apply()的参数为空时,默认调用全局对象。因此,这时的运行结果为0,证明this指的是全局对象。
如果把最后一行代码修改为
  o.m.apply(o); //1
运行结果就变成了1,证明了这时this代表的是对象o。
(完)

2015/01/27

Ubuntu Linux下,安裝與運行 Node.js

---- 安裝 GNU C compiler 和 GNU C++ compiler
To install the gcc and g++ compilers, you will need the build-essential package. This will also install GNU make.

build-essential contains a list of packages which are essential for building Ubuntu packages including gcc compiler, make and other required tools.
$ sudo apt-get update
$ sudo apt-get upgrade
$ sudo apt-get install build-essential
$ sudo apt-get install gccw
$ sudo apt-get install g++
$ gcc -v
$ make -v


---- Linux從源碼安裝Node,才能安裝你所需要的版本

#Ubuntu安裝代碼
$ sudo wget http://nodejs.org/dist/v0.10.35/node-v0.10.35.tar.gz
$ sudo tar -zxf node-v0.10.35.tar.gz #Download this from nodejs.org
$ cd node-v0.10.35
$ sudo ./configure && sudo make && sudo make install
$ sudo cp /usr/local/bin/node /usr/sbin/


---- 測試一下看裝好了沒有,用命令查看node和npm版本
$ node -v
$ npm -v


---- 編寫一個簡單的伺服器體驗一下
var http = require('http');

http.createServer(function (request, response) {
    response.writeHead(200, {'Content-Type': 'text/plain'});
    response.end('Hello World\n');
}).listen(8000);

console.log('Server running at http://localhost:8000/');

• 1、保存代碼到 test1.js
• 2、命令列下執行命令:node test1.js
• 3、通過流覽器訪問 http://localhost:8000/


---- 接著安裝express
$ sudo apt-get install node-express

---- 建立express工程,啟動第一個專案
$ express -e nodejs-demo

install dependencies:
$ cd nodejs-demo && npm install

run the app:
$ node app

---- 安裝curl
$ sudo apt-get install curl
$ curl localhost:3000

Windows 7下,安裝與Debug Node.js

前言:由於Node.js使用VS2010當成C++編譯器,所以建議要安裝VS2010,網路上有人使用如下命令,強迫使用VS2012編譯器

1. 將整個編譯環境強制設成VS2012
$ npm config set msvs_version 2012 --global

2. 安裝時設定編譯器是2012
$ npm install socket.io --msvs_version=2012

node.js 核心基礎課程
Node.js官網

---- 下載 & 執行 Node.js 安裝檔

---- 安裝Brackets
brackets官網 , 下載並安裝

---- 使用Theseus
---- run Brackets (File -- Extension Manager)
---- Available -- type "Theseus" in search box
---- Click the "install"

---- also run "npm install -g node-theseus" to get the command-line helper

---- 安裝Node Inspector
npm install -g node-inspector

---- 安裝Node debug: 
$ npm install -g node-debug

---- 示例
$ node-theseus app.js