Fluent-Style Programming in JavaScript

Fluent-Style Programming in JavaScript

I’ve been playing around with JavaScript a great deal lately and trying to find my way. I last programmed JS seriously about 10 years ago and it’s amazing to me how much the world has changed since then. For example, the fifth edition of ECMAScript (ES5) has recently been approved for standardization and it’s already widely implemented in modern browsers, including my favorite browser, IE9.

Fluent LINQ

However, I’m a big C# fan, especially the fluent API style of LINQ methods like Where, Select, OrderBy, etc. As an example, assume the following C# class:

class Person {
  public Person() { Children = new List<Person>(); }
  public string Name { get; set; }
  public DateTime Birthday { get; set; }
  public int Age { get { return (int)((DateTime.Now - Birthday).Days / 365.25); } }
  public ICollection<Person> Children { get; private set; }
  public override string ToString() { return string.Format("{0} ({1})", Name, Age); }
}

Creating a set of them is a matter of using the C# member initialization syntax:

Five APIs that developers should know about

Posted 10 December 2010 13:08pm by Patricio Robles with 15 comments

Developers have arguably never had it better. A big reason for that: there are virtually countless APIs that enable developers to build really cool applications on top of foundations that someone else invested in laying down.

When it comes to APIs, however, a lot of attention is focused on popular services that have created platforms that developers can tap in to. Facebook and Twitter are two of the most prominent, and many developers have cashed in developing on their platforms.

b

No ifs…alternatives to statement branching in JavaScript

Posted on July 26, 2010 by Angus Croll30 Comments

You could do this..

01

//Example 1

02

function getEventTarget(evt) {

03

    if (!evt) {

04

        evt = window.event;

05

    }

06

    if (!evt) {

07

        return;

08

    }

09

    var target;

10

    if (evt.target) {

11

        target = evt.target;

12

    } else {

13

        target = evt.srcElement;

14

    }

15

    return target;

16

}

or you could do this…

1

//Example 2

2

function getEventTarget(evt) {

3

    evt = evt || window.event;

4

    return evt && (evt.target || evt.srcElement);

5

}


To avoid ambiguity I’ll explain how I’m using a couple of terms in this article:
• 
statement branching: any construct that alters the sequence of statement execution within the global or functional scope. These include if, else, switch, for and while statements.
• 
micro-branching: conditional logic contained within a statement that has no effect on the statement execution seqeunce. The following operators facilitate micro-branching: ternary, && and ||.

OK back to the examples…

Kategória: Nincs kategorizálva | A közvetlen link.

Elnézést, a hozzászólás ezen a részen nem engedélyezett.