下面是我在网上找的一段代码,个人觉得这个书写风格和面向对象很相似,觉得是个不错,所以记一笔吧,如果大家有好的建议,还望多多指教。
function Obj() {
// Initialising code goes here:
alert( 'Loaded!' ); // ...
// Private properties/methods:
var message = 'hello';
sayHello = function() {
alert(message);
};
// Public properties/methods:
this.prop = function() {
sayHello();
};
// Encapsulation: 封装
this.setMessage = function(newMessage) {
message = newMessage;
};
}