包装JavaScript“类”W / out自我执行
Wrapping a JavaScript "class" w/ out self execution?
包装JavaScript "类"?
下面是一个显示消息的简单"类"。它存储输出的HTMLdiv,然后根据需要向它显示消息。
很好。出于可读性和封装的目的,我希望将"类"的组件放在各种容器或JavaScript对象中?最简单的方法是什么?
还是……有什么简单的方法吗?
目前唯一的容器是我用注释包围的代码。
/**
* Module : Model
* Name : -Message
* Input : div via the constructor
message type via display
* Output : TextStream via display to Control.sendInner
* Notes : Symmetric, client holds message location so needs extra variable
*/
var Message = function( div )
{
this.div = document.getElementById( div );
};
Message.prototype.messages =
{
name: 'Please enter a valid name',
email: 'Please enter a valid email',
pass: 'Please enter passoword, 6-40 characters',
url: 'Pleae enter a valid url',
title: 'Pleae enter a valid title',
tweet: 'Please enter a valid tweet',
empty: 'Please complete all fields',
empty_bm: 'Please complete all fields',
same: 'Please make emails equal',
taken: 'Sorry, that email is taken',
validate: 'Please contact <a class="d" href="mailto:support@.com">support</a> to reset your password'
};
Message.prototype.display = function( type )
{
Control.sendInner( this.div, this.messages[type] )
};
/** Message End */
如果我理解正确,您可以使用自调用函数namespace
您的类,如下所示:
(function(window, undefined) {
var foo = 0; //private variable accessible only to code within this scope
//Your class definition:
window.Message = function( div )
{
this.div = document.getElementById( div );
};
Message.prototype.message = //...
//....
})(window);
但也许我误解了这个问题…
我在JS中创建类的常用方法是:(function(window, undefined) {
window.Class = function() {
var private, variables, here;
function privateFunc1(v) { here = v; }
function privateFunc2() { return here; }
return {
publicSet: privateFunc1,
publicGet: privateFunc2
};
}
})(window);
//Usage:
var c1 = new Class(), c2 = new Class();
c1.publicSet(true);
console.log(c1.publicGet(), c2.publicGet()); //output: true, undefined
EDIT修改了一些代码
相关文章:
- Javascript循环不会自我更新
- delete在Object上效率低下,但在DOM Element's的数据属性,与null out相比
- 为什么自我执行匿名有效
- 是否可以在没有外部输入的情况下使函数具有自我意识
- Mouse Out Orange Div "CurrentHeight" is not applyi
- 如何创建对表单的自我更新响应
- getElementsByTagName('img').length logs out to 0
- Javascript/jQuery:当用户Active 值更改时切换 (vjs-fade-out) (video.js)
- JavaScript 自包含函数数组访问自我索引
- 这是来自我的代码还是角度库的 $parse:语法错误
- 让NodeJS应用程序使用NPM进行自我更新
- javascript/angularjs - fade out
- 在AngularJs的SPA中,我如何删除“;.html”;来自我的URL
- 如果自我 === 顶部或 google.com
- 在pink-in上运行js;在pink-out上运行不同的js
- 反应:自我更新的最佳方式,但防止孩子更新
- 应该赞成“_这个“;而不是“;自我;在javascript中
- mouseover,out and click事件不适用于折线
- 使用一个菜单按钮添加animate-in和animate-out类
- 包装JavaScript“类”W / out自我执行