// // 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
发表评论
-
Extending Built-in Types
2009-01-08 13:55 728The Function.apply( ) method is ... -
Breakpoints using closures
2009-01-07 13:43 869// // This function implements ... -
Constructor Functions
2009-01-07 00:42 763the new operator creates a new ... -
Functions as Methods
2009-01-07 00:39 809When a function is invoked as a ... -
The callee Property
2009-01-07 00:02 667In addition to its array elemen ... -
Variable-Length Argument Lists: The Arguments Obje
2009-01-06 23:06 794The Arguments object has one ve ... -
Function Literals
2009-01-06 21:50 705Although function literals crea ... -
Nested Functions
2009-01-06 21:09 769Nested functions may be defined ... -
Deleting Array Elements
2009-01-05 22:11 572The delete operator sets an arr ... -
Reading and Writing Array Elements
2009-01-05 22:00 770Note that array indexes must be ... -
The valueOf() Method
2009-01-05 18:05 748... -
The toLocaleString() Method
2009-01-05 18:02 858In ECMAScript v3 and JavaScript ... -
The constructor Property
2009-01-05 17:51 794[size=medium]Since constructor ... -
the empty statement
2009-01-05 00:29 788[size=medium]When you intention ... -
with
2009-01-05 00:27 686[size=medium]The with statement ... -
try/catch/finnaly
2009-01-05 00:17 892If control leaves the try bloc ... -
throw
2009-01-04 23:56 693The tHRow statement has the fol ... -
return
2009-01-04 23:52 817If a function executes a return ... -
functions
2009-01-04 23:48 799[size=medium]Technically speaki ... -
Labels
2009-01-04 23:01 711Label names are distinct from v ...
相关推荐
### 结构封闭(Structural Closures) #### 引言与背景 《结构封闭》(Structural Closures ch600-1 2005)是美国海军技术手册中的一个章节,更新自2002年12月31日的版本。此章节主要涵盖了船舶上非弹道性水密和气密门...
secrets_of_javascript_closures.pdf
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 ...
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. ...
• 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, ...
A quick summary of closures and callbacks on web development.
访问属性 访问属性 (Accessing Properties)(Accessing Properties) (Accessing Properties) (Accessing Properties) (Accessing Properties)(Accessing Properties) (Accessing Properties)(Accessing Properties)...
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.
let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath) // 使用闭包配置cell cell.textLabel?.text = items[indexPath.row] return cell } ``` 在上述代码中,`cellForRowAt`...
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`是一个非常实用的特性,它允许开发者使用闭包(closures)作为服务容器中的绑定,从而实现更灵活、更动态的依赖注入。本文将深入探讨Laravel开发中的闭包容器及其应用...
### Apress - Java Closures and Lambda (2015):关键知识点解析 #### 引言 本书《Apress - Java Closures and Lambda》聚焦于Java 8中的新特性——闭包(Closures)和Lambda表达式。这些新功能不仅为Java语言带来...
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 范围链和闭包 workshop正在启动$ npm install -g scope-chains-closures$ scope-chains-closures # or, shorter: sccjs使用箭头
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中返回一个闭包函数实例,并提供了相关的代码示例。 Lua中的闭包函数实例通常是指一...