JavaScript Patterns 3.1 Object Literal
Basic concept
Values can be
properties: primitives or other objects
methods: functions
User-defined native objects are mutable at any time.
Object literal notation is ideal for this type of on-demand object creation.
Even the simplest {} object already has properties and methods inherited from Object.prototype.
var dog = {
name: "Benji",
getName: function () {
return this.name;
}
};
- The Object Literal Syntax
• Wrap the object in curly braces ({ and }).
• Comma-delimit the properties and methods inside the object. A trailing comma after the last name-value pair is allowed but produces errors in IE, so don't use it.
• Separate property names and property values with a colon.
• When you assign the object to a variable, don't forget the semicolon after the closing }.
- Objects from a Constructor
// one way -- using a literal var car = {goes: "far"}; // another way -- using a built-in constructor // warning: this is an antipattern var car = new Object(); car.goes = "far";
- Object Constructor Catch
Don't use new Object(); use the simpler and reliable object literal instead.
// Warning: antipatterns ahead // an empty object var o = new Object(); console.log(o.constructor === Object); // true // a number object var o = new Object(1); console.log(o.constructor === Number); // true console.log(o.toFixed(2)); // "1.00" // a string object var o = new Object("I am a string"); console.log(o.constructor === String); // true // normal objects don't have a substring() // method but string objects do console.log(typeof o.substring); // "function" // a boolean object var o = new Object(true); console.log(o.constructor === Boolean); // true
JavaScript Patterns 3.1 Object Literal的更多相关文章
- JavaScript Patterns 5.7 Object Constants
Principle Make variables shouldn't be changed stand out using all caps. Add constants as static prop ...
- JavaScript Patterns 3.4 Array Literal
Array Literal Syntax To avoid potential errors when creating dynamic arrays at runtime, it's much sa ...
- JavaScript Patterns 7.1 Singleton
7.1 Singleton The idea of the singleton pattern is to have only one instance of a specific class. Th ...
- JavaScript Patterns 6.3 Klass
Commonalities • There’s a convention on how to name a method, which is to be considered the construc ...
- JavaScript Patterns 5.3 Private Properties and Methods
All object members are public in JavaScript. var myobj = { myprop : 1, getProp : function() { return ...
- JavaScript Patterns 6.7 Borrowing Methods
Scenario You want to use just the methods you like, without inheriting all the other methods that yo ...
- JavaScript Patterns 6.6 Mix-ins
Loop through arguments and copy every property of every object passed to the function. And the resul ...
- JavaScript Patterns 6.5 Inheritance by Copying Properties
Shallow copy pattern function extend(parent, child) { var i; child = child || {}; for (i in parent) ...
- JavaScript Patterns 6.4 Prototypal Inheritance
No classes involved; Objects inherit from other objects. Use an empty temporary constructor function ...
随机推荐
- python之异常处理
异常处理是高级编程语言必备的一个功能模块. 一.异常基础 在编程过程中为了增加友好性.容错性和健壮性,在程序出现bug时一般不会将错误信息显示给用户,而是现实一个提示的页面,通俗来说就是不让用户看见大 ...
- Qt Style Sheet实践(四):行文本编辑框QLineEdit及自动补全
导读 行文本输入框在用于界面的文本输入,在WEB登录表单中应用广泛.一般行文本编辑框可定制性较高,既可以当作密码输入框,又可以作为文本过滤器.QLineEdit本身使用方法也很简单,无需过多的设置就能 ...
- ok6410 android driver(4)
Install busybox for goldfish/phone 1. Download busybox source code http://www.busybox.net/ 2. Decomp ...
- 数论 --- 费马小定理 + 快速幂 HDU 4704 Sum
Sum Problem's Link: http://acm.hdu.edu.cn/showproblem.php?pid=4704 Mean: 给定一个大整数N,求1到N中每个数的因式分解个数的 ...
- WinForm给控件加入hint文字
本文代码主要是参考别人的,仅为个人记录,方面后续使用~ 效果图: 主要代码在一个Win32Utility类中,代码如下: public static class Win32Utility { [Dll ...
- JavaScript跨域总结与解决办法
什么是跨域 1.document.domain+iframe的设置 2.动态创建script 3.利用iframe和location.hash 4.window.name实现的跨域数据传输 5.使用H ...
- java.lang.NullPointerException org.apache.jsp.WEB_002dINF.pages.imagecheck.test_jsp._jspInit(test_jsp.java:22)的原因
HTTP Status 500 - type Exception report message description The server encountered an internal error ...
- unity3d拓展编辑器MenuItem的使用
MenuItem是自定义菜单栏显示 比如:[MenuItem("new/My Window")] 这样就会显示菜单new/My Window 把这个放在一个静态方法上就可以了.记住 ...
- C#中的接口实现多态
我们都知道虚方法实现多态,抽象方法实现多态等,我们今天来看看如何使用接口实现多态 1.首先我们先要来了解了解什么是接口,它存在的意识 01.接口就是为了约束方法的格式(参数和返回值类型)而存在的 02 ...
- 【iOS】Quartz2D截屏
一.简单说明 在程序开发中,有时候需要截取屏幕上的某一块内容,比如捕鱼达人游戏.如图: 完成截屏功能的核心代码:- (void)renderInContext:(CGContextRef)ctx;调用 ...