JavaScript模式读书笔记 第5章 对象创建模式
<script>
var myApp = {};//通过全局变量来实现命名空间
maApp.Parent = function (){
};
myApp.Child = function(){
};
</script>
<script>
//不安全代码
var myApp = {};
//安全代码
if(typeof myApp === "undefined"){
var myApp = {};
}
//或者使用更短的语句
var myApp = myApp || {};
</script>
<script>
myApp.namespace("myApp.modules.module2");
//等同于如下代码
var myApp = {
modules : {
module2:{}
}
};
</script>
<script>
var myApp = myApp || {};
myApp.namespace = function(ns_string){
var parts = ns_string.split('.'), parent = myApp, i;
if(parent[0] === "myApp"){
parts = parts.slice(1);
}
for(i = 0; i < parts.length; i++){
if(typeof parent[parts[i]] === "undefined"){
parent = parent[parts[i]];
}
}
return parent;
};
var modules2 = myApp.namespace("myApp.modules.module2");
console.log(modules2 === myApp.modules.module2);
myApp.namespace("modules.module2");
</script>
var myFunction = function(){ var event = YAHOO.util.event, dom = YAHOO.util.Dom; }
var myObj = {
myProp : 1,
getPro: function(){
return this.myProp;
}
};
console.log(myObj.myProp);
console.log(myObj.getPro());
<script>
function Gadget(){
//私有成员
var name = "iPoid";
//共有函数
this.getName = function(){
return name;
};
}
var toy = new Gadget();
console.log(toy.name);//undefined
console.log(toy.getName);//iPoid
</script>
<script>
function Gadget(){
//私有成员
var specs = {
screen_width: 320,
screen_hight: 650,
color: 'RED'
};
//共有函数
this.getSpecs = function(){
return specs;
};
}
var toy = new Gadget(),
specs = toy.getSpecs();
specs.color = "GUESS";
console.log(specs.screen_width);//320
console.log(specs.color);//GUESS
console.dir(toy.getSpecs());
</script>
<script>
var myObj = (function(){
var name = "name";
return {
getName: function(){
return name;
}
};
}());
console.log(myObj.getName());//name
</script>
<script>
function Gadget(){
var name = "Ipod";
this.getName = function(){
return name;
};
}
Gadget.prototype = (function(){
var browser = "others";
return{
getBrowser: function(){
return browser;
}
};
}());
var toy = new Gadget();
console.log(toy.getName());//name
console.log(toy.getBrowser());//others
</script>
<script>
var myArray;
(function(){
var astr = "[object Array]",
toString = Object.prototype.toString;
function isArray(s){
return toString.call(s) === astr;
}
function indexOf(haystack, needle){
var i = 0,
max = haystack.length;
for(; i < max; i+= 1){
if(haystack[i] === needle){
return i;
}
}
return -1;
}
myArray = {isArray: isArray,
indexOf: indexOf,
inArray: indexOf};
console.log(myArray.isArray([1, 2]));//true
console.log(myArray.isArray({0 : 1}));//false
console.log(myArray.indexOf(["a", "b"], "a"));//0
console.log(myArray.indexOf(["a", "b"], "d"));//-1
}());
console.log(myArray.indexOf(["a", "b"], "a"));//0
console.log(myArray.indexOf(["a", "b"], "d"));//-1
</script>
<script>
myApp.namespace("myApp.util.array");
myApp.util.array = (function(){
var uobj = myApp.util.object,
ulang = myApp.util.lang,
array_string = "[object Array]",
ops = Object.prototype.toString;
return {
inArray: function(needle, haystack){
for(var i = 0, max = haystack.length; i < max; i+= 1){
if(haystack[i] === needle){
return true;
}
}
},
isArray: function(s){
return ops.call(a) === array_string;
}
};
}());
</script>
<script>
myApp.namespace("myApp.util.array");
myApp.util.array = (function(){
var uobj = myApp.util.object,
ulang = myApp.util.lang,
array_string = "[object Array]",
ops = Object.prototype.toString;
inArray: function(needle, haystack){
for(var i = 0, max = haystack.length; i < max; i+= 1){
if(haystack[i] === needle){
return true;
}
}
},
isArray: function(s){
return ops.call(a) === array_string;
};
return {
isArray: isArray,
indexOf: inArray
};
}());
</script>
<script>
myApp.namespace("myApp.util.array");
myApp.util.array = (function(){
var uobj = myApp.util.object,
ulang = myApp.util.lang;
Constr;
Constr = function(o){
this.elements = this.toArray(o);
};
//共有API原型
Constr.prototype = {
constructor: myApp.util.array,
version: "2.0",
toArray: function(obj){
for(var i = 0, a= [], len = obj.length; i < len; i+= 1){
a[i] = obj[i];
}
return a;
}
};
//返回要分配给新命名空间的构造函数
return Constr;
});
var arr = new myApp.util.array([obj]);
</script>
<script>
MYAPP.util.module = (function(app, global){
//
}(MYAPP, this));
</script>
<script>
new Sandbox(function(box){
//write your code
});
</script>
<script>
Sandbox(['ajax', 'event'], function(box){
//your code
});
</script>
<script>
Sandbox('ajax', 'event', function(box){
//your code
});
</script>
<script>
Sandbox('dom', 'event', function(box){
//your code
Sandbox('ajax', function(box){
//your cide
})
//此处无法获取ajax
});
</script>
<script>
Sandbox.modules = ();
Sandbox.modules.dom = function(box){
box.getElement = function(){};
box.getStyle = function(){};
box.foo = "bar";
};
Sandbox.modules.event = function(box){
//如果需要,可以通过如下语句访问Sandbox原型
//box.constructor.prototype.m = "mmmmm";
box.attachEvent = function(){};
box.dettachEvent = function(){};
};
Sandbox.modules.ajax = function(box){
box.makeRequest = function(){};
boix.getResponse = function(){};
}
</script>
<script>
function Sandbox(){
//将参数转为一个数组
var args = Array.prototype.slice.call(arguments),
//最后一个参数是回调函数
callback = args.pop(),
//模块可以做为一个数组传递,或作为单独的参数传递
modules = (args[0] && typeof args[0] === "string") ? args : args[0],
i;
//确保该函数做为狗仔函数被调用
if(! (this instanceof Sandbox)){
return new Sandbox(modules, callback);
}
//向this添加属性
this.a = 1;
this.b = 2;
//向this对象添加模块
//不制定模块或指定“*”都表示使用所有模块
if(!modules || modules === "*"){
for(i in Sandbox.modules){
if(Sandbox.modules.hasOwnProperty(i)){
moduls.push(i);
}
}
}
//初始化模块
for(i = 0; i < modules.length; i+= 1){
Sandbox.modules[[moduls[i]]](this);
}
callback(this);
}
Sandbox.prototype = {
name: "My Application",
version: "1.00",
getName: function(){
return this.name;
}
};
</script>
<script>
//构造函数
var Gadget = function(){};
//静态方法
Gadget.isShiny = function(){
return "Static Method!";
};
//原型添加方法
Gadget.prototype.setPrice = function(price){
this.price = price;
};
console.log(Gadget.isShiny());//Static Method!
var iphone = new Gadget();
iphone.setPrice(500);
console.log(typeof Gadget.setPrice);//undefined
console.log(typeof iphone.isShiny);//undefined
Gadget.prototype.isShiny = Gadget.isShiny;
console.log(iphone.isShiny());//Static Method!
</script>
<script>
var Gadget = (function(){
var counter = 0;
return function(){
console.log(counter += 1);
}
}());//立即执行
var g1 = new Gadget();//1
var g2 = new Gadget();//2
</script>
myObj.method1("11").method2("w").method3();
var Person = function(name){
this.name = name;
}.method("getName", function(){
return this.name;
});
转载于:https://www.cnblogs.com/jingLongJun/p/4491078.html
JavaScript模式读书笔记 第5章 对象创建模式相关推荐
- 《HeadFirst设计模式》读书笔记-第9章v2-组合模式
定义 组合模式(composite pattern)允许你将对象组合成树形结构来表现"整体/部分"层次结构.组合能让客户以一致的方式处理个别对象以及对象组合. 代码实现 本章使用组 ...
- JavaScript模式读书笔记 第3章 字面量和构造函数
1,对象字面量 -1,Javascript中所创建的自定义对象在任务时候都是可变的.可以从一个空对象开始,根据需要增加函数.对象字面量模式可以使我们在创建对象的时候向其添加函数. ...
- Head First设计模式读书笔记八 第九章上 迭代器模式
之前的总结: https://blog.csdn.net/u011109881/article/details/59677544 个人觉得本章节,HeadFirst讲的没有之前看到的网站讲的清晰,至少 ...
- 《Head First设计模式》读书笔记 -- (第一章)策略模式
本文属贫僧不吃肉原创,欢迎转载,转载请注明来自 http://never-say-never.iteye.com/blog/851923 在开始之前,先熟悉一下OO设计的一些原则. OO设计原则之一: ...
- 《大话设计模式》读书笔记-第8章 工厂方法模式
1.工厂方法(Factory Method),定义一个用于创建对象的接口,让子类决定实例化哪一个类.工厂方法是一个类的实例化延迟到其子类. 2.工厂方法(Factory Method)结构图 3.工厂 ...
- Head First设计模式读书笔记八 第九章下 组合模式
之前的总结链接: https://blog.csdn.net/u011109881/article/details/58710579 对比headFirst书中的例子,我觉得书中的组合模式的例子比上面 ...
- 《HeadFirst设计模式》读书笔记-第9章v3-组合迭代器
定义 组合迭代器不是一个设计模式,是指如何在组合中使用迭代器.所以本章的代码是基于<HeadFirst设计模式>读书笔记-第9章v2-组合模式 修改过来的,需要先熟悉组合模式. 代码实现 ...
- 《JavaScript高级程序设计》读书笔记 -12.1 window对象
<JavaScript高级程序设计>读书笔记 -12.1 window对象 12.1 window对象 12.1.1 Global作用域 12.1.2 窗口关系[不是很懂] 12.1.3 ...
- Java 核心技术卷 II(第 8 版) – 读书笔记 – 第 1 章(下)
22.一旦获得了一个 Charset,就可以在 Java 的 Unicode 和指定的编码格式之间进行转化,下面以 GBK 和 Unicode 之间做为例子. 从 Unicode 到 GBK: imp ...
最新文章
- 字节最新文本生成图像AI,训练集里居然没有一张带文字描述的图片?!
- Transformer 架构逐层功能介绍和详细解释
- 实验0 了解和熟悉操作系统
- Matlab实用程序--图形应用-图形的隐藏属性
- distinct的用法
- 使用Markdown
- HEVC/H265 解码图表,请对照代码看。
- STM32----摸石头过河系列(二)
- 九大内置对象及四个域对象的总结
- 风哥Oracle数据库视频培训教程大合集(网盘下载.共18套)
- 您可能不了解JS:JavaScript圣经的见解
- 计算机平均分函数a,平均值计算函数Average、Averagea、AverageIfs、Trimmean
- ftp客户端软件,Windows端有哪些ftp客户端软件值得推荐?3款ftp客户端软件
- quartz mysql表文件_spring boot+Quartz+数据库存储
- 虚拟机Linux下如何查看自己的IP地址
- 用户需求管理 - KANO模型
- hdu 5234 Happy birthday 背包 dp
- AdGuard免费的电脑手机广告拦截程序
- Java对象 转 JSON 字符串
- 程序测试的逻辑覆盖测试方法
热门文章
- android ble mesh,Android 蓝牙Mesh组网代码详解
- java变量存储位置_java 中变量存储位置的区别
- 计算机协会成立活动简报,“中国计算机学会CCF走进高校”活动在我校举行
- kali linux samba,Kali Linux 渗透测试:SMB、SMTP扫描工具(14)
- ado控件连接oracle,在Delphi 7中用ADOConnection控件连接Oracle 9i的问题
- C语言面试题-这些简单的你能很快的写出来吗?
- 10行Python代码实现Web自动化管控
- Linux那些事儿 之 戏说USB(31)驱动的生命线(三)
- A and B and Lecture Rooms CodeForces - 519E LCA+dfs序
- OpenCV(二十)模板匹配