crockford

What does "use strict" do in javascript, and what is the reasoning behind it?

Recently I ran some of my javascript code through Crockford's JSLint, and it gave the following error: Problem at line 1 character 1: Missing "use strict" statement. Doing some searching, I realized that some people add "use strict"; into their javascript code. And once I added the statement, the error stopped appearing. Unfort...

Javascript: The Good Parts; why is lookahead not good?

I'm reading Douglas Crockfords Javascript: The Good Parts, I just finished the regular expressions chapter. In this chapter he calls javascript's \b, positive lookahead (?=) and negative lookahead (?!) "not a good part" He explains the reason for \b being not good (it uses \w for word boundary finding, and \w fails for any language that...

Prototypal inheritance: Can you chain Object.create?

I'm new to prototypal inheritance so I'm trying to understand the 'right' way. I thought I could do this: if (typeof Object.create !== 'function') { Object.create = function (o) { function F() {} F.prototype = o; return new F(); }; } var tbase = {}; tbase.Tdata = function Tdata() {}; tbase.Tdata.protot...

What are the use cases for closures/callback functions in Javascript?

I was listening to Crockford's talk on Javascript closures and am convinced of the benefit of information hiding, but I do not have a firm understanding of when to use callback functions. It is mostly a true statement that a person could accomplish the same functionality with or without callbacks. As someone who is writing code, what h...

Does margin-left:2px; render faster than margin:0 0 0 2px;?

Douglas Crockford describes the consequence of Javascript inquiring a node's style. How simply asking for the margin of a div causes the browser to 'reflow' the div in the browser's rendering engine four times. So that made me wonder, during the initial rendering of a page (or in Crockford's jargon a "web scroll") is it faster to write...

JavaScript Module Pattern - What about using "return this"?

After doing some reading about the Module Pattern, I've seen a few ways of returning the properties which you want to be public. One of the most common ways is to declare your public properties and methods right inside of the "return" statement, apart from your private properties and methods. A similar way (the "Revealing" pattern) is ...

What is happening in Crockford's object creation technique?

There are only 3 lines of code, and yet I'm having trouble fully grasping this: Object.create = function (o) { function F() {} F.prototype = o; return new F(); }; newObject = Object.create(oldObject); (from Prototypal Inheritance) 1) Object.create() starts out by creating an empty function called F. I'm thinking that a fu...

Using `this` in Crockford's pseudoclassical inheritance pattern

I've just read The Good Parts, and I'm slightly confused about something. Crockford's example of pseudoclassical inheritance goes like this: var Mammal = function (name) { this.name = name; }; Mammal.prototype.get_name = function () { return this.name; }; Part of the problem with this is that the constructor has "its guts han...

Properties attaching to wrong object

I've adapted the Crockford object() function so that I can pass in some parameters and autorun an init function in the new object: function object(o) { function F() {} F.prototype = o; var params = Array.prototype.slice.call(arguments,1); var obj = new F(); if(params.length) { obj.init.apply(obj,params); } ...

Crockford's hanoi function (from "The Good Parts")

Hi, at the moment I'm reading Douglas Crockford's book, and the towers of hanoi function is a bit over my head. Even with logging stuff to the console I wasn't able to really understand what's going on. Here's the function with my additions: var hanoi = function (disc, src, aux, dst) { console.log(disc); console.log(src, dst); ...

The Bad Parts of Crockford's book: "JavaScript: The Good Parts"?

Definitely, Crockford's book: "JavaScript: The Good Parts" is one of the best books in Javascript. However, it is hard to read. I have spent hundreds hours but still cannot understand it completely. Maybe it is not because of Crockford but Javascript itself. I don't care the Good Parts of the book now, but I want to know the Bad Parts o...