`
pstinghua
  • 浏览: 24251 次
  • 性别: Icon_minigender_1
  • 来自: 北京
最近访客 更多访客>>
社区版块
存档分类
最新评论

Private properties with closures

阅读更多
//
// This function adds property accessor methods for a property with 
// the specified name to the object o.  The methods are named get<name>
// and set<name>.  If a predicate function is supplied, the setter
// method uses it to test its argument for validity before storing it.
// If the predicate returns false, the setter method throws an exception.
// 
// The unusual thing about this function is that the property value 
// that is manipulated by the getter and setter methods is not stored in
// the object o.  Instead, the value is stored only in a local variable
// in this function.  The getter and setter methods are also defined
// locally to this function and therefore have access to this local variable.
// Note that the value is private to the two accessor methods, and it cannot
// be set or modified except through the setter.
// 
function makeProperty(o, name, predicate) {
    var value;  // This is the property value

    // The setter method simply returns the value
    o["get" + name] = function() { return value; };

    // The getter method stores the value or throws an exception if
    // the predicate rejects the value
    o["set" + name] = function(v) { 
        if (predicate && !predicate(v))
            throw "set" + name + ": invalid value " + v;        
        else
            value = v;
    };
}

// The following code demonstrates the makeProperty() method 
var o = {};  // Here is an empty object

// Add property accessor methods getName and setName()
// Ensure that only string values are allowed
makeProperty(o, "Name", function(x) { return typeof x == "string"; });

o.setName("Frank");  // Set the property value
print(o.getName());  // Get the property value
o.setName(0);        // Try to set a value of the wrong type
 
分享到:
评论

相关推荐

    Structural Closures (ch600-1) (2005)

    ### 结构封闭(Structural Closures) #### 引言与背景 《结构封闭》(Structural Closures ch600-1 2005)是美国海军技术手册中的一个章节,更新自2002年12月31日的版本。此章节主要涵盖了船舶上非弹道性水密和气密门...

    secrets_of_javascript_closures.pdf

    secrets_of_javascript_closures.pdf

    Mastering Ruby Closures

    The road to Ruby mastery is paved with blocks, procs, and lambdas. To be a truly effective Ruby programmer, it’s not enough just to understand these features—you need to know how to use them in ...

    Java Closures and Lambda(Apress,2015)

    Syntax and usage of the language are changed considerably with the introduction of closures and lambda expressions. This book takes you through these important changes from introduction to mastery. ...

    iOS 8 SDK Development, 2nd Edition

    • Chapter 6, Waiting for Things to Happen with Closures, on pa • Chapter 7, Doing Two Things at Once with Closures, on page • Chapter 8, Growing Our Application, on page 127, • Chapter 9, ...

    closures&callbacks.md

    A quick summary of closures and callbacks on web development.

    Using Swift with Cocoa and Objective-C完整中文CocoaChina精校版

    访问属性 访问属性 (Accessing Properties)(Accessing Properties) (Accessing Properties) (Accessing Properties) (Accessing Properties)(Accessing Properties) (Accessing Properties)(Accessing Properties)...

    OS X App Development with CloudKit and Swift [2016]

    Authors: Wade, Bruce A definitive and practical guide to building apps using ... the differences between var/let, how to work with control statements, closures etc., to work confidently with this book.

    swift-Closures简洁直观的UIKit和Foundation封装库

    let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath) // 使用闭包配置cell cell.textLabel?.text = items[indexPath.row] return cell } ``` 在上述代码中,`cellForRowAt`...

    Mastering.Swift.2.1785886

    12: WORKING WITH CLOSURES 13: USING MIX AND MATCH 14: CONCURRENCY AND PARALLELISM IN SWIFT 15: SWIFT FORMATTING AND STYLE GUIDE 16: NETWORK DEVELOPMENT WITH SWIFT 17: ADOPTING DESIGN PATTERNS IN SWIFT

    Laravel开发-laravel-closures-container

    在Laravel框架中,`laravel-closures-container`是一个非常实用的特性,它允许开发者使用闭包(closures)作为服务容器中的绑定,从而实现更灵活、更动态的依赖注入。本文将深入探讨Laravel开发中的闭包容器及其应用...

    Apress - Java Closures and Lambda.2015

    ### Apress - Java Closures and Lambda (2015):关键知识点解析 #### 引言 本书《Apress - Java Closures and Lambda》聚焦于Java 8中的新特性——闭包(Closures)和Lambda表达式。这些新功能不仅为Java语言带来...

    GObject Reference Manual

    How most people do the same thing with less code How users can abuse signals (and why some think it is good) V. Related Tools GObject builder Graphical inspection of GObjects Debugging reference ...

    scope-chains-closures, Javascript作用域链和闭包 workshop.zip

    scope-chains-closures, Javascript作用域链和闭包 workshop 范围链和闭包 workshop正在启动$ npm install -g scope-chains-closures$ scope-chains-closures # or, shorter: sccjs使用箭头

    Mastering.Swift.1784392154

    Dwell into Subscripts, Optionals, and closures with real-world scenarios Employ Grand Central Dispatch to add concurrency to your applications Study the Objective-C interoperability with mix and match...

    Lua返回一个Closures函数实例

    在Lua中,闭包(Closures)是一种特殊的函数,它可以访问定义它的外部函数的局部变量。在给定的文件信息中,详细介绍了如何在Lua中返回一个闭包函数实例,并提供了相关的代码示例。 Lua中的闭包函数实例通常是指一...

Global site tag (gtag.js) - Google Analytics