util = require 'util'
Q = require 'q'
randomInteger = (base) ->
Math.floor Math.random() * base
class People
constructor: (@name) ->
do: (telephone, callback) ->
util.debug "==> #{@name} #{@type}.do, #{telephone}"
deferred = Q.defer()
timer = setTimeout =>
console.log 'do a long study task'
deferred.resolve telephone;
util.debug "<== #{@name} #{@type}.do"
, randomInteger 3000
deferred.promise
class Student extends People
constructor: (name) ->
super name
@type = 'Student'
class Worker extends People
constructor: (name) ->
super name
@type = 'Worker'
exports.Student = Student
exports.module = Worker # 錯誤寫法
====================
上述exports.modules = Worker宣告當初始該package時, 對外使用介面為Worker function
Worker = require './school'
結果將沒有任何方式可以取得Student function
所以應該要改寫成
exports.Student = Student
exports.Worker = Worker
使用方式如下:
Student = require('school').Student
Worker = require('school').Worker
student = new Student("baby")
worker = new Worker("Rick")
沒有留言:
張貼留言