原创转载请注明出处:http://agilestyle.iteye.com/blog/2341895
Defining Properties
There are two basic ways to create your own objects: using the Object constructor and using an object literal. For example:
var person1 = { name: "Nicholas" }; var person2 = new Object(); person2.name = "Nicholas"; person1.age = "Redacted"; person2.age = "Redacted"; person1.name = "Greg"; person2.name = "Michael";
When a property is first added to an object, JavaScript uses an internal method called [[Put]] on the object.
When a new value is assigned to an existing property, a separate operation called [[Set]] takes place.
See Figure 3-1 for a step-bystep view of what happened to person1 behind the scenes as its name and age properties were changed.
Detecting Properties
A more reliable way to test for the existence of a property is with the in operator.
The in operator looks for a property with a given name in a specific object and returns true if it finds it. In effect, the in operator checks to see if the given key exists in the hash table. For example, here’s what happens when in is used to check for some properties in the person1 object:
console.log("name" in person1); // true console.log("age" in person1); // true console.log("title" in person1); // false
Keep in mind that methods are just properties that reference functions, so you can check for the existence of a method in the same way. The following adds a new function, sayName(), to person1 and uses in to confirm the function’s presence.
var person1 = { name: "Nicholas", sayName: function() { console.log(this.name); } }; console.log("sayName" in person1); // true
In most cases, the in operator is the best way to determine whether the property exists in an object. It has the added benefit of not evaluating the value of the property, which can be important if such an evaluation is likely to cause a performance issue or an error.
In some cases, however, you might want to check for the existence of a property only if it is an own property. The in operator checks for both own properties and prototype properties, so you’ll need to take a different approach. Enter the hasOwnProperty() method, which is present on all objects and returns true only if the given property exists and is an own property. For example, the following code compares the results of using in versus hasOwnProperty() on different properties in person1:
var person1 = { name: "Nicholas", sayName: function() { console.log(this.name); } }; console.log("name" in person1); // true console.log(person1.hasOwnProperty("name")); // true console.log("toString" in person1); // true console.log(person1.hasOwnProperty("toString")); // false
In this example, name is an own property of person1, so both the in operator and hasOwnProperty() return true. The toString() method, however, is a prototype property that is present on all objects. The in operator returns true for toString(), but hasOwnProperty() returns false.
Removing Properties
Just as properties can be added to objects at any time, they can also be removed. Simply setting a property to null doesn’t actually remove the property completely from the object, though. You need to use the delete operator to completely remove a property from an object. The delete operator works on a single object property and calls an internal operation named [[Delete]]. You can think of this operation as removing a key/value pair from a hash table. When the delete operator is successful, it returns true.
var person1 = { name: "Nicholas" }; console.log("name" in person1); // true delete person1.name; // true - not output console.log("name" in person1); // false console.log(person1.name); // undefined
In this example, the name property is deleted from person1. The in operator returns false after the operation is complete. Also, note that attempting to access a property that doesn’t exist will just return undefined. Figure 3-2 shows how delete affects an object.
Reference
Leanpub.Principles.of.Object-Oriented.Programming.in.JavaScript.Jun.2014
相关推荐
Mastering JavaScript Object-Oriented Programming 2016 | ISBN: 1785889109 | English | 287 pages | PDF | 1.9 MB With this book, we'll provide you with a comprehensive overview of OOP principles in ...
If you've used a more traditional object-oriented language, such as C++ or Java, JavaScript probably doesn't seem object-oriented at all. It has no concept of classes, and you don't even need to ...
关于 #define 指令的用法大全 #define 指令是 C 语言中的一种预处理指令,用于给常量赋予一个有意义的名称。在本文中,我们将详细介绍 #define 指令的用法、实例和注意事项。 定义 ---- #define 指令的基本语法有...
在编程世界中,预编译器是程序开发过程中的一个重要环节,它负责处理源代码中的特定指令,如`#include`和`#define`。这些指令在实际编译之前进行处理,以帮助简化和优化源代码。让我们深入探讨一下`#include`和`#...
一般的程序语言,表示空的只有null,但javascript的设计者Brendan Eich却设计了一个undefined,这无疑增加了程序复杂度,但这样做也是有一定原因的。本文将详细介绍javascript中的undefined和null 历史原因1995年...
Object.__define__ = function(Class, fname, func) { var fun = func || Class[fname]; return Class[fname] = { toString: function() { return fun.call(Class); } }; }; ``` 在这个方法中: - `Class`是...
标题中的"object_envi_resize__define_IDl_"很可能是指一个使用IDL(Interactive Data Language)编写的ENVI(Environment for Visualizing Images)扩展程序,该程序专注于图像对象的重采样操作。ENVI是一款广泛...
假设 `x = 1`,则宏调用的结果分别为 `A(1)` ——> `T_1`,`B(1)` ——> `'1'`,`C(1)` ——> `"1"`。这类宏主要用于生成特定的文本或符号。 4. **多行宏定义** 当宏定义需要跨越多行时,必须在每行末尾添加反...
JavaScript Object Walker工具。 安装 > git clone https://github.com/nomocas/owalk.git 或者 > npm install owalk 或者 > bower install owalk 用法 您既可以将其用作浏览器中的AMD模块(requirejs,杏仁等)...
If you've used a more traditional object-oriented language, such as C++ or Java, JavaScript probably doesn't seem object-oriented at all. It has no concept of classes, and you don't even need to ...
define('SCR','index'); 下面加入 include('sstn/mobile_device_detect.php'); mobile_device_detect(false,true,true,'m/index.php',false); 2、上传附件中的mobile_device_detect.php到sstn目录。没有就...
`DEFINE_CG_MOTION`和`DEFINE_GRID_MOTION`是UDF编程中用于定义网格运动的两个关键函数。它们是ANSYS Fluent内建的宏,用于在UDF中指定网格如何随时间变化。 1. `DEFINE_CG_MOTION`:这个宏主要用于定义中心点...
标题中的"DEFINE_DPM_EROSION.rar_DEFINE DPM EROSION_DPM_UDF EROSION_fluent"指的是一款与 Fluent 模拟软件相关的用户定义函数(UDF),用于模拟颗粒动力学(DPM)过程中的侵蚀现象。Fluent 是一款广泛使用的计算...
例子var define = require ( 'define-properties' ) ;var assert = require ( 'assert' ) ;var obj = define ( { a : 1 , b : 2 } , {a : 10 ,b : 20 ,c : 30} ) ;assert ( obj . a === 1 ) ;assert ( obj . b === 2...
标题中的"Define_Grid_Motion.zip_DEFINE_GRID_MOTION_fluent udf_fluent变形_f" 提到了`Fluent`中的`Define Grid Motion`功能,这正是用来定义网格运动的一种方法,它允许用户自定义边界条件下的网格动态行为。...
#define cJSON_Object 6 #define cJSON_IsReference 256 #define cJSON_StringIsConst 512 /* The cJSON structure: */ typedef struct cJSON { struct cJSON *next,*prev; /* next/prev allow you to walk ...
标题中的"FLUENT UDFs 金属材料属性udf,表面张力、DEFINE-PROPERTY、DEFINE-SPECIFIC-HEAT"涉及了UDFs在模拟金属材料特性的应用,特别是关于不锈钢的热力学性质。 首先,我们来看`DEFINE-PROPERTY`和`DEFINE-...
"define-frozen-property"是一个这样的开源库,它专注于定义并冻结JavaScript对象的属性。冻结属性是一个重要的概念,它涉及到JavaScript的原型继承、对象属性特性以及数据安全等方面。在这篇文章中,我们将深入探讨...
Part IV is a reference for client-side JavaScript, covering legacy web browser APIs, the standard Level 2 DOM API, and emerging standards such as the XMLHttpRequest object and the <canvas> tag. ...
使用场合:拼接两个宏,一个是传入的宏。但是传入的宏不会被替换,反而原封不动的接了上去,这就尴尬了。...#define PARAM DEFINE(OBJECT) void fun() { // DEFINE_(OBJECT)=100; 这个操作是拒绝的,它就是直接拼