- 浏览: 45747 次
- 性别:
- 来自: 湖南
最新评论
-
laj12347:
javascript 闭包真是牛逼呀。。看了好久,终于有点 ...
JavaScript: The Good Parts 读书笔记(二) -
mu0001:
赞一个,很不错。以现在的知识来看,先看了第一部分。
JavaScript 数据访问(翻译自High Performance Javascript 第二章) -
e421083458:
得买本书好好看看了!
JavaScript 数据访问(翻译自High Performance Javascript 第二章) -
adamed:
这本书已经有中文版出版了。。到处都可以买到。高性能 javas ...
JavaScript 数据访问(翻译自High Performance Javascript 第二章) -
jiyanliang:
elvishehai 写道来源于那里的呀,写的很不错,学习了. ...
JavaScript 数据访问(翻译自High Performance Javascript 第二章)
二.Object
- Object Constructor Catch
You have no reason to use the new Object() constructor when you can use an object literal, but you might be inheriting legacy code written by others, so you should be aware of one “feature” of this constructor (or yet another reason not to use it). The feature in question is that the Object() constructor accepts a parameter and, depending on the value passed, it may decide to delegate the object creation to another built-in constructor and return a different object than you expect. Following are a few examples of passing a number, a string, and a boolean value to new Object(); the result is that you get objects created with a different constructor:// 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
- Constructor’s Return Values
When invoked with new, a constructor function always returns an object; by default it’s the object referred to by this. If you don’t add any properties to this inside of your constructor, an “empty” object is returned (“empty” aside from inheriting from the constructor’s prototype).
Constructors implicitly return this, even when you don’t have a return statement in the function. But you can return any other object of your choosing. In the next example, a new object referenced by that is created and returned.var Objectmaker = function () { // this `name` property will be ignored // because the constructor // decides to return another object instead this.name = "This is it"; // creating and returning a new object var that = {}; that.name = "And that's that"; return that; }; // test var o = new Objectmaker(); console.log(o.name); // "And that's that"
As can you see, you have the freedom to return any object in your constructors, as long as it’s an object. Attempting to return something that’s not an object (like a string or a boolean false, for example) will not cause an error but will simply be ignored, and the object referenced by this will be returned instead(!).
- Self-Invoking Constructor
To address the drawback of forgeting "new" when construct a new instance and have prototype properties available to the instance objects, consider the following approach. In the constructor you check whether this is an instance of your constructor, and if not, the constructor invokes itself again,this time properly with new:function Waffle() { if (!(this instanceof Waffle)) { return new Waffle(); } this.tastes = "yummy"; } Waffle.prototype.wantAnother = true; // testing invocations var first = new Waffle(), second = Waffle(); console.log(first.tastes); // "yummy" console.log(second.tastes); // "yummy" console.log(first.wantAnother); // true console.log(second.wantAnother); // true
- Array Constructor Curiousness
One more reason to stay away from new Array() is to avoid a possible trap that this constructor has in store for you.
When you pass a single number to the Array() constructor, it doesn’t become the value of the first array element. It sets the length of the array instead. This means that new Array(3) creates an array with length of 3, but no actual elements. If you try to access any of the elements, you get the value undefined because the elements don’t exist. The following code example shows the different behavior when you use the literal and the constructor with a single value.// an array of one element var a = [3]; console.log(a.length); // 1 console.log(a[0]); // 3 // an array of three elements var a = new Array(3); console.log(a.length); // 3 console.log(typeof a[0]); // "undefined"
Although this behavior might be a little unexpected, it gets worse when you pass a floating point number to new Array() as opposed to an integer. This results in an error because the floating point is not a valid value for the array’s length:// using array literal var a = [3.14]; console.log(a[0]); // 3.14 var a = new Array(3.14); // RangeError: invalid array length console.log(typeof a); // "undefined"
To avoid potential errors when creating dynamic arrays at runtime, it’s much safer to stick with the array literal notation.
- Check for Array-ness
Using the typeof operator with array operands returns “object.”console.log(typeof [1, 2]); // "object"
Although this behavior makes sense (arrays are objects), it’s not too helpful. Often you need to know if a value actually is an array. Sometimes you can see code checking for the presence of length property or some array method such as slice() to determine “array-ness.” But these checks are not robust because there’s no reason why a nonarray object shouldn’t have properties and methods with the same names. Also people sometimes use instanceof Array, but this check works incorrectly when used across frames in some IE versions. ECMAScript 5 defines a new method Array.isArray(), which returns true if the argument is an array. For example:Array.isArray([]); // true // trying to fool the check // with an array-like object Array.isArray({ length: 1, "0": 1, slice: function () {} }); // false
If this new method is not available in your environment, you can make the check by calling the Object.prototype.toString() method. If you invoke the call() method of toString in the context of an array, it should return the string “[object Array]”. If the context is an object, it should return the string “[object Object]”. So you can do something like this:if (typeof Array.isArray === "undefined") { Array.isArray = function (arg) { return Object.prototype.toString.call(arg) === "[object Array]"; }; }
- Error Objects
JavaScript has a number of built-in error constructors, such as Error(), SyntaxError(),TypeError(), and others, which are used with the throw statement. The error objects created by these constructors have the following properties:
• name
The name property of the constructor function that created the object; it could be the general “Error” or a more specialized constructor such as “RangeError”
• message
The string passed to the constructor when creating the object
The error objects have other properties, such as the line number and filename where the error happened, but these extra properties are browser extensions inconsistently implemented across browsers and are therefore unreliable.
On the other hand, throw works with any object, not necessarily an object created with one of the error constructors, so you can opt in for throwing your own objects. Such error objects can have the properties “name,” “message,” and any other type of information you want to pass to be handled by the catch statement. You can be creative when it comes to your custom error objects and use them to restore the application state back to normal.try { // something bad happened, throw an error throw { name: "MyErrorType", // custom error type message: "oops", extra: "This was rather embarrassing", remedy: genericErrorHandler // who should handle it }; } catch (e) { // inform the user alert(e.message); // "oops" // gracefully handle the error e.remedy(); // calls genericErrorHandler() }
The error constructors invoked as functions (without new) behave the same as constructors (with new) and return the same error objects.
发表评论
-
High Performance JavaScript 读书笔记(五)
2011-02-27 21:24 2276五.Strings and Regular Expressio ... -
High Performance JavaScript 读书笔记(六)
2011-02-27 20:49 2661六.Responsive Interfaces There ... -
High Performance JavaScript 读书笔记(四)
2011-02-27 19:50 1216四.Algorithms and Flow Control ... -
High Performance JavaScript 读书笔记(三)
2011-02-27 14:43 1727三.DOM Scripting DOM scripting ... -
High Performance JavaScript 读书笔记(一)
2011-02-27 13:44 1612一.Loading and Execution JavaS ... -
JavaScript 数据访问(翻译自High Performance Javascript 第二章)
2011-02-27 13:21 1932计算机科学中一 ... -
JavaScript Patterns 读书笔记(七)
2011-02-15 20:32 14437.Client Pattern DOM Access ... -
JavaScript Patterns 读书笔记(六)
2011-02-15 19:38 12626.Design Pattern Singleton ... -
JavaScript Patterns 读书笔记(五)
2011-02-15 19:08 12335.Inheritance Pattern Classic ... -
JavaScript Patterns 读书笔记(四)
2011-02-15 18:19 11884.Function Pattern Namespace ... -
JavaScript Patterns 读书笔记(三)
2011-02-14 21:19 1463三.Function Background The ... -
JavaScript Patterns 读书笔记(一)
2011-02-14 20:09 1662一.Global 全局域中的this = window.m ... -
JavaScript: The Good Parts 读书笔记(五)
2011-01-27 12:56 2091五.Javascript 总结 语 ... -
JavaScript: The Good Parts 读书笔记(四)
2011-01-27 11:37 1002四.数组与正则表达式 ... -
JavaScript: The Good Parts 读书笔记(三)
2011-01-26 23:38 1408三.继承 概述 Javascr ... -
JavaScript: The Good Parts 读书笔记(二)
2011-01-26 23:01 1784二.函数 JS 中函数亦是对象 使用字面变量表面 ... -
JavaScript: The Good Parts 读书笔记(一)
2011-01-26 21:44 1290一.基础特性 在浏览器中,每个<script> ...
相关推荐
其次,笔记可能会深入讲解设计模式(Design Patterns),这是软件设计中的重要概念,如工厂模式、单例模式、观察者模式等。这些模式为解决常见的软件设计问题提供了标准化的解决方案,是提高代码复用性和灵活性的...
这篇博客涵盖了前端开发的多个重要知识点,包括心法、LeetCode算法练习、框架原理、计算机网络、设计模式、自造轮子以及读书笔记等。以下是对这些内容的详细阐述: **心法**: 在前端开发中,心法指的是开发者应该...
它包含一些经典的GoF设计模式的集合,这些模式使用JavaScript和ES6类以及Node.js API在JavaScript中实现。 感谢并非常欢迎您做出贡献,调整现有模式的README.md,或改进示例或添加新模式。 只需做出一个叉子,进行...
本笔记主要依据曾探的著作《JavaScript设计模式与开发实践》进行整理,涵盖了设计模式的基本知识,以及创建、结构、行为、状态和策略等模式的应用。 **基础知识** 在深入设计模式之前,我们需要理解一些基本概念,...
2. `languages/` - 不同编程语言的学习笔记,如Python、Java、JavaScript等。 3. `frameworks/` - 使用的各种框架的笔记,如Django、React、Angular等。 4. `algorithms/` - 数据结构和算法的学习笔记,包括实现和...
2. **GWT_speakernoted.pdf**:这可能是关于Google Web Toolkit (GWT)的演讲笔记,GWT是一个开源的Java开发工具,它允许开发者使用Java编写客户端Web应用,然后编译成优化的JavaScript代码。GWT支持Ajax特性,并且...
8. **Design_Patterns**:讨论软件设计模式,如单例模式、工厂模式等在前端开发中的应用。 9. **Interview_Tips**:提供面试技巧,如何回答行为问题,如何展示自己的项目经验等。 10. **Projects**:可能包含一些...
都是参考众多文章归纳整理学习而写的,文章包括了HTML基础、CSS基础、JavaScript基础与拓展、Browser浏览器相关、Vue使用与分析、React使用与分析、Plugin插件相关、Patterns设计模式、Linux命令、LeetCode题解等...
都是参考众多文章归纳整理学习而写的,文章包括了HTML基础、CSS基础、JavaScript基础与拓展、Browser浏览器相关、Vue使用与分析、React使用与分析、Plugin插件相关、Patterns设计模式、Linux命令、LeetCode题解等...
除了每日一题的学习笔记之外还有一些做项目时的记录以及遇到的坑等,对于技术相关的文章基本都是参考众多文章归纳整理学习而写的,文章包括了HTML基础、CSS基础、JavaScript基础与拓展、Browser浏览器相关、Vue使用...
项目介绍小路描述CSS CSS世界练习代码UIAndAnamited 画布,SVG,三等练习代码算法算法练习成分组件封装练习源代码原始解析笔记数据结构斑点结构DesignPatterns |设计模式演示自己实现的一些好玩的小演示
女性笔记 :clapping_hands:欢迎star,也不容易搞丢链接哦! :clapping_hands: 欢迎加入明星,失去联系并不容易!它是什么?我是在参加2020年秋招的时候,实际上那时候往前2个月我才开始系统接触前沿领域。为了快速...
单页图书项目使用项目的IT笔记(Java,Linux,密码学,云,体系结构等)的超级备忘单。克隆并执行下一行以启动本地Web服务器。 $ ./httpServer.py仅需要服务器,以避免浏览器禁用从文件系统路径加载脚本的麻烦。 ...