« Previous entry | Next entry » Browse > Snippets

Skip to comments (18) Some cool javascript stuff
Posted by Erik on Jan 08 2006 @ 13:54  :: 3744 unique visits

First a nice usefull javascript statment which for some reason is deprecated in the latest javascript versions.
The with statment allows you to execute code in another scope:
CODE: JAVASCRIPT
with (Math) {
   var a = floor(12 / PI); // Instead of Math.floor and Math.PI
}

with (window.opener) {
  ...
}


The window.opener is usefull for popups to execute code in the scope of the opener's window (the base class for everything).

----------------------------------------------------------------------------------------------------------------------------------------------------

Because in javascript functions are first-class objects and javascript supports Closures we can do stuff like this:
CODE: JAVASCRIPT
function repeater(n) {
  return function(w) {
    for (var r = '', i = 0; i < n; i++) {
      r += w;
    }
    return r;
  }
}

var rep5 = repeater(5);

alert(rep5('a')); // will show aaaaa
 


----------------------------------------------------------------------------------------------------------------------------------------------------

Another cool but almost never used feature of javascript is that you can toString() a function. This can be used to modify a function at runtime:
CODE: JAVASCRIPT
var foo = 1;
var bar = 2;

function test() {
  return foo;
}

alert(test()); // will show 1

eval(test.toString().replace('foo', 'bar'));

alert(test()); // wil show 2
 

18 comments posted so far
Add your own »

1. On Jan 09 2006 @ 16:31 Erik wrote:

The repeater function can also be written in C++ by overloading the function application operator of a class called repeater:
CODE: CPP
class repeater {
private:
    int n;

public:
    repeater(int _n) : n(_n) { ; }

    std::string operator () (std::string w) const {
        std::string r;
        for (int i = 0; i < n; i++) {
            r += w;
        }
        return r;
    }
};


The usage is as followed:
CODE: CPP
repeater rep5(5);

printf(rep5("a").c_str()); // will print aaaaa
 

2. On Jan 10 2006 @ 19:07 Erik wrote:

Note that the above (repeater) construction is called Currying.

The example from the wikipedia article but now written in C++:
CODE: CPP
int plus(int x, int y) {
    return x + y;
}


class curried_plus {
private:
    int x;

public:
    curried_plus(int _x) : x(_x) { ; }

    int operator () (int y) const {
        return plus(x, y);
    }
};


curried_plus plus_one(1);
plus_one(2); // returns 3
 

3. On May 13 2009 @ 15:44 guest wrote:

Need Furniture? And need to buy furniture from China at competitive price? LongYear Furniture is your source for quality bedroom furniture featuring a huge selection of home furniture a happy home for beautiful life, kids furniture for a good memory of childhood, and to gain an extra good price from wholesale furniture and direct from  furniture manufacturers China, styles of China furniture are available, living room furniture is also nice for your house, find dining room furniture and more!

5. On Jul 14 2009 @ 04:03 guest wrote:

buy wow gold
my wow power leveling
buy wow gold
good wow power leveling
BUY wow gold
my wow power leveling
CHEAP rs gold
cheap wow power leveling
CHEAPEST lotro gold
MY aion gold
buy wow gold
cheap wow gold
CHEAPEST wow gold

7. On Oct 27 2009 @ 10:05 Nikeairmax wrote:

www.airmaxmall.com

10. On Jan 05 2010 @ 14:28 uggbaileybutton wrote:

bailey button uggs

-ugg boots cheap

ugg boots uk

ugg classic

11. On Jan 10 2010 @ 09:28 ugg14785 wrote:

Ugg Boots,also called Ug or Ugg boots,are nowadays one of the most popular boots.They existed already in 1930's and became popular in 1960's when Australian surfers found out,that they can warm their feet after coming out from cold water.They have everything you need-comfort and warmth.You will never be cold in those boots because 100% sheepskin will warm you even in the coldest days.The natural thermal properties of sheepskin make possible that Ugg Boots will warm you in the winter but in the summer your feet will not be wet.It is true that Ugg is shorter from ugly,but if you know even little bit about fashion you will know that Ugg boots are not ugly.We can see famous people like Pamela Anderson or Kate Hudson wearing Ugg Boots.

Nowadays we have new improved Ugg boots for sale.You can get them for anybody.We have women boots,children boots,slippers and casuals.

Our UGG Australia Boots are all authentic.Their properties and features as below:

*In cold temperatures,the fleece works as an insulating layer of warmth and keeps the warmth of body heat inside of the boot.
*In the heat,the wool fibers keep the air circulating; thus keeping your fee dry and pulling the perspiration away from your skin.Sheepskin also contains lanolin,which moisturizes your feet when worn barefoot.
*ugg boots have reinforced toes,which are double stitched and extra durable heels.
*feature a natural,thick fleece on a removable in-sole for arch support.
*The sole is usually durable in most terrain,lightweight and provides traction as well.
welcome to www.bootscardy.com

12. On Jan 10 2010 @ 09:29 ugg14785 wrote:

We have been exporter of UGG Australia Boots for many years,our product are all authentic.Welcome to buy any boots of them in our website,we charge the shipping fee.
We just take 7 days to deliver the products to your door.

As everyone knows,UGG Australia Boots in australia, and many other countries as well, ugg boots also ug or uggs are a style of footwear that has been used generically for well over a hundred years.Everywhere you go now you find that the ugg boot has become the latest fashion trend.

Our UGG Australia Boots are all with top quality:

*100% authentic sheepskin for unrivalled comfort
*flexible molded EVA outsole
*Insole features a genuine sheepskin sockliner that naturally wicks away moisture and keeps feet dry
*Wool blend knit uppers with suede heel guards
*Approximate Shaft Height (Uncuffed): 14.5"
*By UGG Australia;Imported.

  welcome to www.bootsdept.com

13. On Jan 10 2010 @ 09:30 ugg14785 wrote:

The original ugg boots is not a brand name but an age old sheepskin boots generic term for this style of australian-made sheepskin boot. in Aussie slang, the ug name is short for “ugly.” in terms of comfort, however, the ug boot is a thing of beauty.Ugg boots are a popular brand of boot for women,and children.just as the chairman of australian ugg said, australian ugg is a good choice. once wearing it, you will be reluctant to take it off and be fascinated by its originality, reliability and extremely luxurious comfort. this is the art brought by boots. ugg boots make young beautiful girls and handsome boys very confident.

Our website have many fashion styels of UGG Boots for women and children.

We provide many advantages as below:
*free shipping
*hidden costs
*professional customers service
*secure processing,plus
*we dont hassle you on returns.if you dont like what youve received, simply return it!

UGG Boots must be you best choice!
welcome to www.bootshotsale.com

14. On Jan 10 2010 @ 09:31 ugg14785 wrote:

When heard of winter, cold may come in your mind at once.if you are ready for the coming of winter,you should never be afraid of it.it is not difficult for us that if you protect your feet well,you will fell warm and comfortable.so you should get several shoes help you spend the cold winter.UGG Boots can be done.for many individuals,ugg boots provide the perfect insulation and comfort for the winter months.ugg boots are both warm and comfortable.these two factors are extremely important,no matter what amount of time you are going to be spending out in the weather.ugg boots are made out of a material that is water resistant, meaning that no water,and no cold air,can get into your boots.they will keep in the warmth of your feet,because of their outer shell and their inner linings. once you slip your feet into a pair of these boots,they will be warm and toasty until it is time to take them off.you will be able to take advantage of the great warmth that ugg boots offer.

Welcome to buy UGG Boots in our website,we offer the UGG Australia boots are with top quality:
*features genuine twin-face sheepskin for refreshing comfort
*feature a soft foam insole covered with genuine sheepskin
*have a molded EVA light and flexible outsole designed for amazing comfort with every step.
*you can enjoy free shipping worldwide in our website.
*1 week delivery to your door
*sometimes you can get a special discount from our company by email


Our customers are always satisfied with such kind of UGG Boots and our high service!

welcome to www.dressugg.com

15. On Jan 10 2010 @ 09:31 ugg14785 wrote:

Legend has it that "ugg" is short for "ugly" - so, "ugg boots" apparently meant "ugly boots." For decades, "ugg" was generic term used for sheepskin boots made in Australia.The word was so commonplace that it was found in Australian dictionaries, and ugg boots were also known as "ug" or "ugh" boots. But in the United States, when people refer to "UGG boots," they're usually talking about the boots made by Deckers Outdoor Corporation, the parent company of "UGG Australia."So if you're traveling the world and talking about footwear, it's good to know that when the term "ugg boot" is used, it could be referring to an Australian-made sheepskin boot, or a popular brand name boot.

We sell authentic UGG Boots in our website for many year.We offer the best service and favourable price.People all aound the world buy our products do not pay for the shipping,we charge it!

Our UGG Boots have the advantages below:
* 100% authentic sheepskin for unrivalled comfort
* genuine twin-face sheepskin for refreshing comfort
* have a molded EVA light and flexible outsole designed for amazing comfort with every step.
*You can wear them up or style them folded down

Welcome to buy UGG Boots in our website,we offer the UGG Australia boots are all with top quality!
welcome to www.e-uggs.com

16. On Jan 10 2010 @ 09:32 ugg14785 wrote:

what's the main point of the boots?the answer is to keep your feet warm.If you by one pair of boots put them on,especially in winter,you'll feel very tenderness and comfortable.they can make your feet warm.and youll find its really fashionable.you can tie-in each kind of clothes with them and still looks beautifull.

The ugg boots will definitely make you noticeable in a fashion-conscious crowd,and more importantly,is the fact that these boots are indeed quite comfortable to wear. these boots have been popular with swimmers and surfers since the 1970s and are regularly worn by surfers in california.the design of the boot with its sheepskin results in considerable thermostatic benefits.


We sell real UGG Australia Boots in our website.You must be satisfied with our UGG Boots when you receive your order.The UGG Boots you get will have these advatages:
*100% authentic sheepskin for unrivalled comfort
*a durable molded rubber outsole.
*The wrap around lace feature adds a fashionable touch with wooden beads at the end
*A removable and replaceable sheepskin insole gives extra comfort and warmth.
*Insole is made of sheepskin,latex.
welcome to  www.famousuggs.com

17. On Jan 10 2010 @ 09:33 ugg14785 wrote:

Sheepskin and ugg boots are a casual type of boot. Ugg boots are exceptionally warm and are great for cold weather (up to -30 degrees Fahrenheit)If you are looking for warmth while trudging around the house or the city, a sheepskin or ugg boot will be ideal.For girls and women, ugg boots look fantastic with tight-legged jeans tucked into them, or with short skirts paired with opaque tights or bare legs. For guys, ugg boots work great with jeans, and give a very casual, or rugged look, depending on the jeans or the shirt you wear with your ugg boots.

Do you have a pair of UGG Boots?If not,you must will buy a piar in our website.As we offer the best sevice and favourable price.what's more,we charge the shipping fee.So,you just need to pay the fee of boots,and will receive our UGG boots in one week.It is very easy to have your own UGG Boots.Come on!

Our UGG Boots are all with top quality:
*features genuine twin-face sheepskin
*our signature UGG woven label
*feature a soft foam insole covered with genuine sheepskin
*have a molded EVA light and flexible outsole designed for refreshing comfort with every step
welcome to www.findugg.com

18. On Jan 10 2010 @ 09:33 ugg14785 wrote:

Everywhere you go now you find that the ugg boot has become the latest fashion new arrival cheap trend. now days it seems that you can not walk pass any window display of any shoe or fashion clothing store with seeing a pair of these trendy boots in them. it does not matter where ever you are in the world you will find ugg boots. these boots are both hot and trendy and certainly do not need to be worn in cold weather only. many people have found, that one pair of ugg boots is not enough.

We have been seller of UGG Boots for many years.Our customers are satisfied with our products as we offer the real UGG Australia Boots:
*100% authentic sheepskin for unrivalled comfort
*soft genuine sheepskin heel padding
*cushioning insole,flexible fabric-trimmed midsole.
*Lightweight molded EVA traction outsole
*Free shipping
*One week to your door
*If you dont like what youve received, simply return it!
welcome to  www.fineugg.com

Add a new comment

Name:
Password: (leave empty for anonymous comment)
 
View formatting tags Comment: