这是一个简单的示例,将新行为动态添加到现有对象或Decorator模式中。由于动态语言(例如Javascript)的性质,这种模式成为语言本身的一部分。// Person object that we will be decorating with logging capabilityvar person = { name: "Foo", city: "Bar"};// Function that serves as a decorator and dynamically adds the log method to a given objectfunction MakeLoggable(object) { object.log = function(property) { console.log(this[property]); }}// Person is given the dynamic responsibility hereMakeLoggable(person);// Using the newly added functionalityperson.log('name');