⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 6.04 - using callbacks.js

📁 JS设计模式源代码
💻 JS
字号:
// Accessor without function callbacks: returning requested data in accessors.window.API = window.API || {};API.prototype = function() {  var name = 'Hello world';  // Privileged mutator method.  setName: function(newName) {    name = newName;    return this;  },  // Privileged accessor method.  getName: function() {    return name;  }}();// Implementation code.var o = new API;console.log(o.getName()); // Displays 'Hello world'.console.log(o.setName('Meow').getName()); // Displays 'Meow'.// Accessor with function callbacks.window.API2 = window.API2 || {};API2.prototype = function() {  var name = 'Hello world';  // Privileged mutator method.  setName: function(newName) {    name = newName;    return this;  },  // Privileged accessor method.  getName: function(callback) {    callback.call(this, name);    return this;  }}();// Implementation code.var o2 = new API2;o2.getName(console.log).setName('Meow').getName(console.log);// Displays 'Hello world' and then 'Meow'.

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -