SeaJS结合javascript面向对象使用笔记(一)

Sea.JS

Seajs结合javascript面向对象

所需页面

/app/index.html

/lib/factory.js

/lib/sea.js

/lib/constructor.js

/js/init.js

代码

Index.html

<!DOCTYPE html>

<html>

<head>

    <title>面向对象</title>

    <meta charset="utf-8" />

</head>

<script src="../lib/sea.js"></script>

<body>

<script type="text/javascript">

    seajs.use("../js/init.js");

</script>

</body>

</html>

 

 

Factory.js

define(function(require,exports){

//工厂方式

 function createObject(name,age) {

        var o = new Object();

        o.name = name;

        o.age = age;

        o.say = function() {

            alert(this.name+"-"+this.age);}

        return o;

    }

 exports.createObject=createObject;

});

Constructor.js

define(function(require,exports){

 //构造函数方式

    function Person_constructor(name,age) {

        this.name = name;

        this.age = age;

        this.say=function(){

            alert(this.name+"="+this.age);

        }

    }

 

    exports.Person = Person_constructor;

});

 

 

Init.js

//init.js

define(function(require, exports, module) {

 var factory = require('../lib/factory') ;

  var constructor = require('../lib/constructor');

 //工厂方式创建对象

    var b = factory.createObject("ajck",11);

//构造函数方式

    var b2 = new constructor.Person("leno",22);

 

    //解决工厂方式不能使用instanceof判断对象,因为工厂方式创建instanceof Object

    console.info(b2 instanceof constructor.Person);

    b2.say();});

原文地址:https://www.cnblogs.com/or2-/p/3316635.html