nodejs类从module里面导出的多种方法

这里特指从module导出时的类内部共享变量和方法的写法。

方法一:

lib/test.js

var kuohao = {
    "v1" : "i am v1",
    "f2" : function () {
      this.f1()
    },
    "f1" : function () {
        console.log("show in f1: " + this.v1)
    }
}
exports = module.exports = kuohao;

test.js

var testlib = require("./lib/test")
testlib.f2()

变形一下:

var kuohao = {
    "v1" : "i am v1",
    "f2" : function () {
        kuohao.f1()
    },
    "f1" : function () {
        console.log("show in f1: " + kuohao.v1)
    }
}
exports = module.exports = kuohao;

方法二:

lib/test.js

var kuohao = function () {
    this.v1 = "i am v1";
    this.f1 = function () {
        console.log("show in f1: " + this.v1)
    }
    this.f2 = function () {
        this.f1()
    }
}
exports = module.exports = new kuohao;

test.js

var testlib = require("./lib/test")
testlib.f2()

以上,如果是导出时不加new(exports = module.exports = kuohao;)

那么使用时就要再new一下。

test.js

var testlib = require("./lib/test")
var testlib = new testlib()
testlib.f2()

还可以变种一下,使用可不加new

lib/test.js

exports = module.exports = new function () {
    this.v1 = "i am v1";
    this.f1 = function () {
        console.log("show in f1: " + this.v1)
    }
    this.f2 = function () {
        this.f1()
    }
}

方法三:

lib/test.js

function Kuohao() {}
Kuohao.prototype.v1 = "i am v1";
Kuohao.prototype.f1 = function () {
    console.log("show in f1: " + this.v1)
}
Kuohao.prototype.f2 = function () {
    this.f1()
}
exports = module.exports = new Kuohao();

test.js

var testlib = require("./lib/test")
testlib.f2()

以上方法的变量稍微换一下方式:

lib/test.js

function Kuohao() {
    this.v1 = "i am v1";
}
Kuohao.prototype.f1 = function () {
    console.log("show in f1: " + this.v1)
}
Kuohao.prototype.f2 = function () {
    this.f1()
}
exports = module.exports = new Kuohao();

方法四

lib/test.js

function Kuohao() {
    var kuohao = new Object();
    kuohao.v1 = "i am v1";
    kuohao.f1 = function () {
        console.log("show in f1: " + this.v1)
    }
    kuohao.f2 = function () {
        this.f1()
    }
    return kuohao;
}
exports = module.exports = Kuohao();

test.js

var testlib = require("./lib/test")
testlib.f2()

稍微变种一下,效果一样
lib/test.js

function Kuohao() {
    var kuohao = function () {};
    kuohao.v1 = "i am v1";
    kuohao.f1 = function () {
        console.log("show in f1: " + this.v1)
    }
    kuohao.f2 = function () {
        this.f1()
    }
    return kuohao;
}
exports = module.exports = Kuohao();

参数传递

lib/test.js

function Kuohao(_arg) {
    console.log(_arg)
    var kuohao = new Object();
    kuohao.v1 = "i am v1";
    kuohao.f1 = function () {
        console.log("show in f1: " + this.v1)
    }
    kuohao.f2 = function () {
        this.f1()
    }
    return kuohao;
}
exports = module.exports = Kuohao;

test.js

var testlib = require("./lib/test")("i am arg")
testlib.f2()

方法五
lib/test.js

var Kuohao = function () {
    function Kuohao() {
        this.v1 = "i am v1";
    }
    Kuohao.prototype.f1 = function f1() {
        console.log("show in f1: " + this.v1)
    }
    Kuohao.prototype.f2 = function f2() {
        this.f1()
    }
    return Kuohao;
}();
exports = module.exports = Kuohao;

test.js

var testlib = require("./lib/test")
var obj = new testlib();
obj.f2();

方法六

lib/test.js

var options = {
    f1 : function () {
        console.log("show in f1: " + this.v1)
    },
    f2 : function () {
        this.f1()
    }
};
function Kuohao() {
    this.v1 = "i am v1";
}
Kuohao.prototype = options;
exports = module.exports = new Kuohao();

test.js

var testlib = require("./lib/test")
testlib.f2();

方法七

lib/test.js

var v1 = "i am v1";
exports.f1 = function() {
    console.log("show in f1: " + v1)
};
function f2() {
    exports.f1()
}
exports.f2 = f2;

test.js

var testlib = require("./lib/test")
testlib.f2();

你可能感兴趣的:(编程笔记)