};
that.says = function () {
return spec.saying || '';
};
return that;
};
var myMammal = mammal({name: 'Herb'});
var cat = function (spec) {
spec.saying = spec.saying || 'meow';
var that = mammal(spec);
that.purr = function (n) {
//purr
};
that.getName = function () {
return that.says() ' ' spec.name ' ' that.says();
};
return that;
};
var myCat = cat({name: 'Henrietta'});
8. Array var colors=["Red","Yellow","Blue"];
var value=colors[0]; //"Red"
Arrays, like objects, are key-value collections. The difference is that arrays can use integers as attribute names. Arrays also provide a very useful set of built-in methods.
Every array has a length property. The value of the length attribute is the largest integer attribute name of this array plus 1. It is not necessarily equal to the number of attributes in the array.
9. Regular expression
var numberRegex=/ ^-?d (?:.d*)?(?:e[ -]?d )?$/i;
numberRegex.test(1.2); //true
Regular Expression grouping:
() Capturing grouping
(?:) Non-capturing grouping
Regular expression escape:
\ / [ ] ( ) { } ? * | . ^ $
f form feed character
n line feed character
r carriage return character
t tab character
uXXXX Unicode character specified by 4-digit hexadecimal XXXX
d matches a number (equivalent to in [0-9])
1 Reference to capture group 1 (2 and so on)
Regular expression class escape:
- \ / [ ] ^
b Backspace character
f form feed character
n line feed character
r carriage return character
t tab character
uXXXX Unicode character specified by 4-digit hexadecimal XXXX
d matches a number (equivalent to in [0-9])
Regular expression quantifier:
? Match 0 or 1 times (same as {0,1})
* Match 0 or more times (same as {0,})
Match 1 or more times (same as {1,})
{n} Match n times
{n,} Match at least n times
{n,m} Match at least n times, but not more than m Times
regular expression flags:
g performs global matching (all matches)
i performs case-insensitive matching
m performs multi-line matching (^ and $ can match line terminators)