person.js

/**
* This example make use of requireJS to provide a clean and simple way to split JavaScript class definitions
* into separate files and avoid global namespace pollution. http://requirejs.org/
*
* We start by defining the definition within the require block inside a function; this means that any
* new variables / methods will not be added to the global namespace; requireJS simply requires us to return
* a single value (function / Object) which represents this definition. In our case, we will be returning
* the Class' function.
*/
define(function () {
// Forces the JavaScript engine into strict mode: http://tinyurl.com/2dondlh
"use strict"; /**
* This is our classes constructor; unlike AS3 this is where we define our member properties (fields).
* To differentiate constructor functions from regular functions, by convention we start the function
* name with a capital letter. This informs users that they must invoke the Person function using
* the `new` keyword and treat it as a constructor (ie: it returns a new instance of the Class).
*/
function Person(name) {
// This first guard ensures that the callee has invoked our Class' constructor function
// with the `new` keyword - failure to do this will result in the `this` keyword referring
// to the callee's scope (typically the window global) which will result in the following fields
// (name and _age) leaking into the global namespace and not being set on this object.
if (!(this instanceof Person)) {
throw new TypeError("Person constructor cannot be called as a function.");
} // Here we create a member property (field) for the Person's name; setting its value
// what the one supplied to the Constructor. Although we don't have to define
// properties ahead of time (they can easily be added at runtime as all Object / functions
// in JavaScript are dynamic) I believe it makes your code easier to follow if you list your
// classes intentions up front (eg: in the Constructor function).
this.name = name; // Here we are defining a private member. As there is no `private` keyword in JavaScript
// there is no way for us to hide this data (without resorting to inelegant hacks); instead
// we choose to use a naming convention where a leading underscore indicates a property
// is private and should not be relied upon as part of the Classes public API.
this._age = -1;
} /**
* Adding static properties is as simple as adding them directly to the constructor
* function directly.
*/
Person.RETIREMENT_AGE = 60; /**
* Public Static methods are defined in the same way; here's a static constructor for our Person class
* which also sets the person's age.
*/
Person.create = function (name, age) {
var result = new Person(name);
result.setAge(age); return result;
}; /**
* Any functions not added to the Person reference won't be visible, or accessible outside of
* this file (closure); however, these methods and functions don't belong to the Person class either
* and are static as a result.
*/
function formatNameAndAge(person) {
// Note that `this` does not refer to the Person object from inside this method.
if (person._age === -1) {
return "We don't know how old " + person.name + " is!";
} return (person.name + ", is " + person._age + " years old and "
+ ((person.canRetire()) ? "can" : "can't") + " retire");
}; /**
* The prototype is a special type of Object which is used as a the blueprint for all instances
* of a given Class; by defining functions and properties on the prototype we reduce memory
* overhead. We can also achieve inheritance by pointing one classes' prototype at another, for
* example, if we introduced a BankManager class which extended our Person class, we could write:
*
* `BankManager.prototype = Person.prototype`
* `BankManager.prototype.constructor = BankManager`
*
* However, due to the dynamic nature of JavaScript I am of the opinion that favouring composition
* over inheritance will make your code easier to read and re-use.
*/
Person.prototype = { /**
* Whenever you replace an Object's Prototype, you need to repoint
* the base Constructor back at the original constructor Function,
* otherwise `instanceof` calls will fail.
*/
constructor: Person, /**
* All methods added to a Class' prototype are public (visible); they are able to
* access the properties and methods of the Person class via the `this` keyword. Note that
* unlike ActionScript, usage of the `this` keyword is required, failure to use it will
* result in the JavaScript engine trying to resolve the definition on the global object.
*/
greet: function () {
// Note we have to use the `this` keyword.
return "Hello, " + this.name;
}, /**
* Even tho the `_age` property is accessible; it still makes a lot of sense to provide
* mutator methods (getters / setters) which make up the public API of a Class - here we
* validate the supplied value; something you can't do when a field is modified directly
*/
setAge: function (value) {
// Ensure the supplied value is numeric.
if (typeof (value) !== 'number') {
throw new TypeError(typeof (value) + " is not a number.");
} // Ensure the supplied value is valid.
if (isNaN(value) || value < 0) {
throw new RangeError("Supplied value is out of range.");
} this._age = value;
}, /**
* This method access both a member property and a static property.
*/
canRetire: function() {
return this._age >= Person.RETIREMENT_AGE;
}, /**
* Finally we can also access 'static' functions and properties.
*/
toString: function() {
// Note that as `formatNameAndAge` is static we must supply a reference
// to `this` so it can operate on this instance.
return formatNameAndAge(this);
}
}; // As mentioned up top, requireJS needs us to return a value - in this files case, we will return
// a reference to the constructor function.
return Person;
});

  使用:

    require(['site'],function(Person){
var jonny = new Person("Jonny");
jonny.setAge(29);
console.log(jonny.toString());
});

  

 

requirejs amd module load example的更多相关文章

  1. 解决metasploit的module load fail

    解决metasploit的module load fail 在exploits文件夹下面新建一个文件夹test 把你要用的rb文件放进去 reload_all 就行了

  2. 模块化 Sea.js(CMD)规范 RequireJS(AMD)规范 的用法

    插入第三方库AMD CMD都 一样  如:JQ(再JQ源码里修改) 使用seajs的步骤 1.HTML里引入seajs <script src="./lib/sea.js"& ...

  3. 对于requirejs AMD模块加载的理解

    个人人为使用模块化加载的优点有三: 1,以我的项目为例:90%为图表展示,使用的是echarts,此文件较大,requirejs可以在同一个版本号(urlArgs)之下缓存文件,那么我就可以在访问登陆 ...

  4. requireJs,AMD,CMD

    知识点1:AMD/CMD/CommonJs是JS模块化开发的标准,目前对应的实现是RequireJs/SeaJs/nodeJs.   知识点2:CommonJs主要针对服务端,AMD/CMD主要针对浏 ...

  5. Javascript Module pattern template. Shows a class with a constructor and public/private methods/properties. Also shows compatibility with CommonJS(eg Node.JS) and AMD (eg requireJS) as well as in a br

    /** * Created with JetBrains PhpStorm. * User: scotty * Date: 28/08/2013 * Time: 19:39 */ ;(function ...

  6. AMD及requireJS

    前面的话 由CommonJS组织提出了许多新的JavaScript架构方案和标准,希望能为前端开发提供统一的指引.AMD规范就是其中比较著名一个,全称是Asynchronous Module Defi ...

  7. 详解AMD规范及具体实现requireJS在工程中的使用

    前面的话 由CommonJS组织提出了许多新的JavaScript架构方案和标准,希望能为前端开发提供统一的指引.AMD规范就是其中比较著名一个,全称是Asynchronous Module Defi ...

  8. RequireJS API

    可以找到许多的解读,但是原文总是最重要的,也是最正宗的说明,直接访问 RequireJS 有时不太方便,这里将 RequireJS 2.0 API 的原文转载到博客园,方便查看. This is th ...

  9. JavaScript AMD 模块加载器原理与实现

    关于前端模块化,玉伯在其博文 前端模块化开发的价值 中有论述,有兴趣的同学可以去阅读一下. 1. 模块加载器 模块加载器目前比较流行的有 Requirejs 和 Seajs.前者遵循 AMD规范,后者 ...

随机推荐

  1. Codeforces 1017E The Supersonic Rocket 凸包,计算几何,字符串,KMP

    原文链接https://www.cnblogs.com/zhouzhendong/p/CF1017E.html 题目传送门 - CF1017E 题意 给定两个点集,并构成两个凸包. 问这两个凸包是否可 ...

  2. Linux安装Tomcat-Nginx-FastDFS-Redis-Solr-集群——【第八集之安装Nginx】

    1,务必保证安装Nginx之前,所需的环境必须安装完备.  gcc 安装nginx需要先将官网下载的源码进行编译,编译依赖gcc环境,如果没有gcc环境,需要安装gcc:yum install gcc ...

  3. Linux安装Tomcat-Nginx-FastDFS-Redis-Solr-集群——【第三集之磁盘分区】

    磁盘分区的概念对接下来的自定义安装Linux具有重要作用.(可以直接先看第四集之Linux安装就能知道分区的重要性) ----------------------------------------- ...

  4. 防止vs编译时自动启动单元测试

    Tools → Options → Live Unit Testing   Pause 勾选

  5. Python编程基础(一)

    1.Python中的变量赋值不需要类型声明 2.等号(=)用来给变量赋值 3.字符串拼接用  “+”  号 temp=‘123’ print('temp的值是%s'%temp) #整数和字符创的转换, ...

  6. java添加多个水印

    package com.zhx.util.imgutil; import com.zhx.util.stringutil.ArithUtil; import net.coobird.thumbnail ...

  7. CentOS系统找不到setup命令工具的解决方法

    如果你的CentOS系统中没有setup命令,很有可能是因为你安装CentOS系统时采用了最小化安装(minimal).这时,你执行setup命令时,就会报错: 错误信息: 1[root@localh ...

  8. C++雾中风景13:volatile解惑

    笔者入职百度时,二面面试官的让我聊聊C++之中的volatile关键词.volatile在Java和C++之中的差别可谓是天差地别,我只是简单聊了聊Java之中的volatile,面试官对我的回答并不 ...

  9. JS的异步世界

    前言 JS的异步由来已久,各种异步概念也早早堆在开发者面前.可现实代码中,仍然充斥了各种因异步顺序处理不当的bug,或因不好好思考,或因不了解真相.今天,就特来再次好好探索一番JS的异步世界. 01 ...

  10. Spring BPP中优雅的创建动态代理Bean

    一.前言 本文章所讲并没有基于Aspectj,而是直接通过Cglib以及ProxyFactoryBean去创建代理Bean.通过下面的例子,可以看出Cglib方式创建的代理Bean和ProxyFact ...