JavaScript 10分钟速成 (js-in-ten-minutes)

JavaScript 10分钟速成 (js-in-ten-minutes)
JavaScript 10分钟速成 (js-in-ten-minutes)

Javascript in Ten Minutes

Spencer Tipping

December22,2010

Contents

1Introduction3 2Types3 3Functions4

3.1Variadic behavior(a cool thing) (4)

3.2Lazy scoping(a cool thing) (4)

3.3The meaning of this(the egregious disaster) (5)

3.3.1Important consequence:eta-reduction (6)

4Gotchas7

4.1Semicolon inference (7)

4.2Void functions (7)

4.3var (8)

4.4Lazy scoping and mutability (8)

4.5Equality (9)

4.6Boxed vs.unboxed (9)

4.7Things that will silently fail or misbehave (10)

4.8Things that will loudly fail (11)

4.9Throwing things (11)

4.10Be careful with typeof (11)

4.11Also be careful with instanceof (12)

4.12Browser incompatibilities (13)

5Prototypes13

5.1Why new is awful (14)

5.2Why you should use prototypes (15)

5.3Autoboxing (15)

6A Really Awesome Equality15

1

7If You Have20Minutes (16)

7.1Forwarding constructors (17)

7.2Iterators for cool people (17)

7.3Java classes and interfaces (18)

7.4Recursive metaclasses (19)

7.5Tail calls and delimited continuations (20)

7.6Syntactic macros and operator overloading (22)

8Further reading24

2

1Introduction

This guide is for anyone who knows some Javascript but would like a quick1

intro to its advanced features.It will be easier reading if you also know another functional language such as Ruby,Perl,Python,ML,Scheme,etc,since I don’t

really explain how?rst-class functions work.

2Types

Javascript has nine types.They are:

1.Null–null.Chucks a wobbly if you ask it for any attributes;e.g.null.foo

fails.Never boxed.2

2.Unde?ned–undefined.What you get if you ask an object for something

it doesn’t have;e.g.document.nonexistent.Also chucks a wobbly if you

ask it for any attributes.Never boxed.

3.Strings–e.g.’foo’,"foo"(single vs.double quotation marks makes no

di?erence).Sometimes boxed.Instance of String when boxed.

4.Numbers–e.g.5,3e+10(all numbers behave as?oats–signi?cant for

division,but can be truncated by x>>>0).Sometimes boxed.Instance

of Number when boxed.

5.Booleans–true and false.Sometimes boxed.Instance of Boolean when

boxed.

6.Arrays–e.g.[1,2,"foo",[3,4]].Always boxed.Instance of Array.

7.Objects–e.g.{foo:’bar’,bif:[1,2]},which are really just hashta-

bles.Always boxed.Instance of Object.

8.Regular expressions–e.g./foo\s*([bar]+)/.Always boxed.Instance

of RegExp.

9.Functions–e.g.function(x){return x+1}.Always boxed.In-

stance of Function.

The value null is actually almost never produced by Javascript.The only

case you’re likely to run across null is if you assign it somewhere(most of the

time you’ll get undefined instead–one notable exception is document.getElementById, which returns null if it can’t?nd an element).Making sparing use of undefined

and instead using null can make bugs much easier to track down.

1Longer than ten minutes,despite what the title says.

2Boxing is just a way of saying whether something has a pointer.A boxed type is a reference

type,and an unboxed type is a value type.In Javascript,this has additional rami?cations as well–

see section4.6.

3

3Functions

Functions are?rst-class lexical closures,3just like lambda s in Ruby or sub s in

Perl.4They behave pretty much like you’d expect,but there are several really

cool things about functions and one really egregious disaster.

3.1Variadic behavior(a cool thing)

Functions are always variadic.5Formal parameters are bound if they’re present;

otherwise they’re undefined.For example:

(function(x,y){return x+y})(’foo’)//=>’fooundefined’The arguments to your function can be accessed in a?rst-class way,too:

var f=function(){return arguments[0]+arguments[1]};

var g=function(){return arguments.length};

f(’foo’)//=>’fooundefined’

g(null,false,undefined)//=>3

The arguments keyword is not an array!It just looks like one.In particular,

doing any of these will cause problems:

arguments.concat([1,2,3])

[1,2,3].concat(arguments)

arguments.push(’foo’)

arguments.shift()

To get an array from the arguments object,you can say Array.prototype.slice.call(arguments). As far as I know that’s the best way to go about it.

3.2Lazy scoping(a cool thing)

Internally,functions use a lexical scoping chain.However,the variables inside

a function body aren’t resolved until the function is called.This has some really

nice advantages,perhaps foremost among them self-reference:

var f=function(){return f};

f()===f//=>true

3First-class in the sense that you can pass them around as values at runtime.You can’t reliably

introspect them,however,because while you can obtain their source code via toString you won’t

be able to access the values they close over.

4Note that block scoping isn’t used–the only scopes that get introduced are at function bound-

aries.

5The number of arguments a function accepts is referred to as its arity.So a unary function,

which is monadic,takes one,a binary function,which is dyadic,takes two,etc.A function that

takes any number of arguments is said to be variadic.

4

Tidbit of pathology:An important consequence of lazy scoping is

that you can create functions that refer to variables that might never

exist.This makes Javascript very di?cult to debug.The good part

is that Javascript can be made to support syntactic macros via the

toString method:

var f=function(){return$0+$1};

var g=eval(f.toString().replace(/\$(\d+)/g,

function(_,digits){return’arguments[’+digits+’]’}));

g(5,6)//=>11(except on IE)

Theoretically by extending this principle one could implement true

structural macros,operator overloading,a type system,6or other

things.

3.3The meaning of this(the egregious disaster)

One would think it is a simple matter to?gure out what this is,but it’s apparently quite challenging,and Javascript makes it look nearly impossible.

Outside of functions(in the global scope,that is),the word this refers to the

global object,which is window in a browser.The real question is how it behaves

inside a function,and that is determined entirely by how the function is called.

Here’s how that works:

1.If the function is called alone,e.g.foo(5),then inside that function’s body

the word this will be equivalent to the global object.

2.If the function is called as a method,e.g.x.foo(5),then inside that

function’s body the word this refers to the object,in this case x.

3.If the function starts o?as a method and then is called alone:

var f=x.foo;

f(5);

then this will be the global object again.Nothing is remembered about

where f came from;it is all determined right at the invocation site.

4.If the function is invoked using apply or call,then this points to what-

ever you set it to(unless you try to set it to null or undefined,in which

case it will be the global object again):

var f=function(){return this};

f.call(4)//=>4

f.call(0)//=>0

f.call(false)//=>false

f.call(null)//=>[object global]

6God forbid.

5

Given this unpredictability,most Javascript libraries provide a facility to set a function’s this binding(referred to within Javascript circles as just a function’s binding)to something invocation-invariant.The easiest way to do this is to de?ne a function that proxies arguments using apply and closes over the proper value(luckily,closure variables behave normally):

var bind=function(f,this_value){

return function(){return f.apply(this_value,arguments)};

};

The di?erence between call and apply is straightforward:f.call(x,y,z) is the same as f.apply(x,[y,z]),which is the same as bind(f,x)(y,z). That is,the?rst argument to both call and apply becomes this inside the func-tion,and the rest are passed through.In the case of apply the arguments are expected in an array-like thing(arguments works here),and in the case of call they’re passed in as given.

3.3.1Important consequence:eta-reduction

In most functional programming languages,you can eta-reduce things;that is,if you have a function of the form function(x){return f(x)},you can just use f instead.But in Javascript that’s not always a safe transformation; consider this code:

Array.prototype.each=function(f){

for(var i=0,l=this.length;i

f(this[i]);

};

var xs=[];

some_array.each(function(x){xs.push(x)});

It might be tempting to rewrite it more concisely as:

some_array.each(xs.push);

however this latter form will result in a mysterious Javascript error when the native Array.push function?nds this to be the global object instead of xs. The reason should be apparent:when the function is called inside each,it is invoked as a function instead of a method.The fact that the function started out as a method on xs is forgotten.(Just like case3above.)

The simplest way around this is to bind xs.push to xs:

some_array.each(bind(xs.push,xs));

6

4Gotchas

Javascript is an awesome language just like Perl is an awesome language and Linux is an awesome operating system.If you know how to use it properly,it will solve all of your problems trivially(well,almost),and if you miss one of its subtleties you’ll spend hours hunting down bugs.I’ve collected the things I’ve run into here,which should cover most of Javascript’s linguistic pathology.7 4.1Semicolon inference

You won’t run into any trouble if you always end lines with semicolons.How-ever,most browsers consider it to be optional,and there is one potential surprise lurking if you choose to omit them.

Most of the time Javascript does what you mean.The only case where it might not is when you start a line with an open-paren,like this:

var x=f

(y=x)(5)

Javascript joins these two lines,forming:

var x=f(y=x)(5)

The only way around this that I know of is to put a semicolon on the end of the?rst line.

4.2Void functions

Every function returns a value.If you don’t use a return statement,then your function returns undefined;otherwise it returns whatever you tell it to.This can be a common source of errors for people used to Ruby or Lisp;for instance, var x=(function(y){y+1})(5);

results in x being undefined.If you’re likely to make this slip,there’s an Emacs mode called“js2-mode”that identi?es functions with no side-e?ects or return values,and it will catch most of these errors.8

7There is plenty of this pathology despite Javascript being generally an excellent language.This makes it ideal both for people who want to get things done,and for bug-connoisseurs such as myself.

8Of course,that’s if you’re an Emacs person.If you prefer a real editor(wink),I wrote a custom JS highlighter that handles some cases better than the builtin one:https://www.360docs.net/doc/326680318.html,/ spencertipping/js-vim-highlighter.

7

4.3var

Be careful how you de?ne a variable.If you leave o?the var keyword,your variable will be de?ned in the global scope,which can cause some very subtle bugs:

var f=function(){//f is toplevel,so global var x=5;//x is local to f

y=6;//y is global

};

As far as I know,the same is true in both types of for loop:

for(i=0;i<10;++i)//i is global

for(var i=0;i<10;++i)//i is local to the function

for(k in some_object)//k is global

for(var k in some_object)//k is local to the function

4.4Lazy scoping and mutability

This is a beautiful disaster.Check this out:

var x=[];

for(var i=0;i<3;++i)

x[i]=function(){return i;};

x[0]();//What will these be?

x[1]();

x[2]();

What will our three functions return when they are eventually called?You might expect them to return0,1,and2,respectively,since those were the values of i when they were created.However they will actually all return3.This is because of Javascript’s lazy scoping:Upon creation,each function receives only a variable name and a scope in which to search for it;the value itself is not resolved until the time of invocation,at which point i will equal3.

The simplest way to?x this is to wrap our assignment in an anonymous function that is evaluated immediately,introducing another layer of scope.The following code works because within the enclosing anonymous function,the value of new_i never changes.

for(var i=0;i<3;++i)

(function(new_i){

x[new_i]=function(){return new_i;};

})(i);

By the way,you might be tempted to do this:

8

for(var i=0;i<3;++i){

var j=i;

x[j]=function(){return j;};

}

This won’t work for same reason that our original example failed:j will be scoped to the nearest enclosing function(remember that Javascript’s scoping is function level,not block level!),so its value is changing just as frequently as i’s.

4.5Equality

Because the==is lame,these are all true in Javascript:

null==undefined

null==0

false==’’

’’==0

true==1

true==’1’

’1’==1

So,never use the==operator unless you really want this behavior.Instead, use===(whose complement is!==),which behaves sensibly.In particular,=== requires both operands to not only be the same-ish,but also be of the same type. It does referential comparison for boxed values and structural comparison for unboxed values.If one side is boxed and the other is unboxed,===will always return false.Because string literals are unboxed,though,you can use it there:’foo’===’fo’+’o’.

Tidbit of pathology:It turns out that==isn’t even stable under

truthiness.If x=0and y=new Number(0),then x==y,!!x is

false,and!!y is true.Section4.6talks more about why this kind

of thing happens.

4.6Boxed vs.unboxed

Boxed values are always truthy and can store properties.Unboxed values will silently fail to store them;for example:9

var x=5;

x.foo=’bar’;

x.foo//=>undefined;x is an unboxed number.

var x=new Number(5);

x.foo=’bar’;

x.foo//=>’bar’;x is a pointer.

9There are other consequences of boxing;see sections4.10and4.11for some examples.

9

How does a sometimes-boxed value acquire a box?When you do one of these things:

1.Call its constructor directly,as we did above

2.Set a member of its prototype and refer to this inside that method(see

section5)

All HTML objects,whether or not they’re somehow native,will be boxed.

4.7Things that will silently fail or misbehave

Javascript is very lenient about what you can get away with.In particular,the following are all perfectly legal:

[1,2,3].foo//=>undefined

[1,2,3][4]//=>undefined

1/0//=>Infinity

0*’foo’//=>NaN

This can be very useful.A couple of common idioms are things like these: e.nodeType||(e=document.getElementById(e));

options.foo=options.foo||5;

Also,the language will convert anything to a string or number if you use+. All of these expressions are strings:

null+[1,2]//=>’null1,2’

undefined+[1,2]//=>’undefined1,2’

3+{}//=>’3[object Object]’

’’+true//=>’true’

And all of these are numbers:

undefined+undefined//=>NaN

undefined+null//=>NaN

null+null//=>0

{}+{}//=>NaN

true+true//=>2

0+true//=>1

And some of my favorites:

null*false+(true*false)+(true*true)//=>1

true<4

true/null//=>Infinity

10

4.8Things that will loudly fail

There is a point where Javascript will complain.If you call a non-function, ask for a property of null or undefined,or refer to a global variable that doesn’t exist,10then Javascript will throw a TypeError or ReferenceError.By extension,referring to local variables that don’t exist causes a ReferenceError, since Javascript thinks you’re talking about a global variable.

4.9Throwing things

You can throw a lot of di?erent things,including unboxed values.This can have some advantages;in this code for instance:

try{

...

throw3;

}catch(n){

//n has no stack trace!

}

the throw/catch doesn’t compute a stack trace,making exception processing quite a bit faster than usual.But for debugging,it’s much better to throw a proper error:

try{

...

throw new Error(3);

}catch(e){

//e has a stack trace,useful in Firebug among other things

}

4.10Be careful with typeof

Because it behaves like this:

typeof function(){}//=>’function’

typeof[1,2,3]//=>’object’

typeof{}//=>’object’

typeof null//=>’object’

typeof typeof//hangs forever in Firefox typeof is a really lame way to detect the type of something in many cases.11 Better is to use an object’s constructor property,like this:

10To get around the error for this case,you can say typeof foo,where foo is the potentially nonexistent global.It will return’undefined’if foo hasn’t been de?ned(or contains the value undefined).

11And because it returns a string,it’s marginally slower than using.constructor.

11

(function(){}).constructor//=>Function

[1,2,3].constructor//=>Array

({}).constructor//=>Object

true.constructor//=>Boolean

null.constructor//TypeError:null has no properties In order to defend against null and undefined(neither of which let you ask

for their constructor),you might try to rely on the falsity of these values:

x&&x.constructor

But in fact that will fail for’’,0,and false.The only way I know to get around this is to just do the comparison:

x===null||x===undefined?x:x.constructor

Alternatively,if you just want to?nd out whether something is of a given type,you can just use instanceof,which never throws an exception.12

4.11Also be careful with instanceof

instanceof is generally more useful than typeof,but it only works with boxed values.For example,these are all false:

3instanceof Number

’foo’instanceof String

true instanceof Boolean

However,these are all true:

[]instanceof Array

({})instanceof Object

[]instanceof Object//Array inherits from Object

/foo/instanceof RegExp//regular expressions are always boxed (function(){})instanceof Function

One way to work around the?rst problem is to wrap primitives:

new Number(3)instanceof Number//true

new String(’foo’)instanceof String//also true

new Boolean(true)instanceof Boolean//also true In general,(new x.constructor(x)instanceof x.constructor)will be true for all primitive x.However,this doesn’t hold for null or undefined. These will throw errors if you ask for their constructors,and as far as I know

are never returned from the result of a constructor invocation(using new,that is).

12Well,almost.If you ask for it by putting null,undefined,or similarly inappropriate things on

the right-hand side you’ll get a TypeError.

12

4.12Browser incompatibilities

Generally browsers since IE6have good compatibility for core language stu?. One notable exception,however,is an IE bug that a?ects String.split:

var xs=’foo bar bif’.split(/(\s+)/);

xs//on reasonable browsers:[’foo’,’’,’bar’,’’,’bif’] xs//on IE:[’foo’,’bar’,’bif’]

A more subtle bug that took me several hours to?nd is that IE6also doesn’t return functions from eval():

var f=eval(’function(){return5}’);

f()//on reasonable browsers:5

f()//on IE6:’Object expected’(because f is undefined) I’m sure there are other similar bugs out there,though the most common ones to cause problems are generally in the DOM.13

5Prototypes

I used to have a very anti-OOP comment here,but considering that I occa-sionally use prototypes I removed it.Despite my obvious and probably unfair vendetta against Javascript’s linguistic compromises to pander to Java-inspired marketing pressure,14prototype-based programming can be useful on occasion. This section contains my subjective and biased view of it.

Whenever you de?ne a function,it serves two purposes.It can be what every normal programmer assumes a function is–that is,it can take values

and return values,or it can be a mutant instance-generating thing that does something completely di?erent.Here’s an example:

//A normal function:

var f=function(x){return x+1};

f(5)//=>6

This is what most people expect.Here’s the mutant behavior that no rational person would ever imagine:

//A constructor function

var f=function(x){this.x=x+1};//no return!

var i=new f(5);//i.x=6

The following things are true at this point:

13jQuery is your friend here.It’s branded as a Javascript library,but in fact it’s a set of enhance-ments to the DOM to(1)achieve a uniform cross-browser API,and(2)make it easier to retrieve

and manipulate nodes.

14Hence its name,Java script,despite all of the dissimilarities.

13

i.constructor===f

i.__proto__===i.constructor.prototype//on Firefox,anyway

i instanceof f

typeof i===’object’

The new keyword is just a right-associative(pre?x)unary operator,so you

can instantiate things?rst-class:

var x=5;

new x.constructor();//Creates a boxed version of x,regardless of what x is new new Function(’x’,’this.x=5’);

If you are going to program using this questionable design pattern,then

you’ll probably want to add methods to things:15

var f=function(x){this.x=x};

f.prototype.add_one=function(){++this.x};

var i=new f(5);

i.add_one();

i.x//=>6

You can?nd tons of information about this kind of prototype programming

online.

5.1Why new is awful

new has some cool features(such as being?rst-class),but it has a really horrible

shortcoming.Most functions in Javascript can be forwarded–that is,you can

write a new function to wrap an existing one and the function being called will

never know the di?erence.For example:

var to_be_wrapped=function(x){return x+1};

var wrapper=function(){

return to_be_wrapped.apply(this,arguments);

};

//for all x,wrapper(x)===to_be_wrapped(x)

However,new has no such mechanism.You can’t forward a constructor in

the general case,because new has no equivalent of apply.(Though this isn’t the

whole story–see section7.1for a way hack to do it.)

15This section used to say that i.x would evaluate to7.That isn’t true though.It’s actually6,as

indicated.(Thanks to Daniel Gasparotto for pointing this out.)

14

5.2Why you should use prototypes

If you need a dynamic-dispatch pattern,then prototypes are probably your best bet and you should use them rather than a roll-your-own approach.Google’s V8 has a bunch of prototype-speci?c optimizations,as do later releases of Firefox. Also,prototypes save memory;having a pointer to a prototype is much cheaper than having n pointers to n attributes.

If,on the other hand,you?nd yourself implementing actual inheritance hierarchies,then you’re probably making a mistake.16I have found prototypes to be an e?ective way to program in Javascript,but inheritance in Javascript is (1)slow,17and(2)poorly representative of Javascript’s“everything is public”model.

5.3Autoboxing

You might be tempted to try something like this:18

Boolean.prototype.xor=function(rhs){return!!this!==!!rhs};

And,upon running this code,you’d run into this tragically unfortunate property:

false.xor(false)//=>true

The reason is that when you treat an unboxed value as an object(e.g.invoke one of its methods),it gets temporarily promoted into a boxed value for the purposes of that method call.This doesn’t change its value later,but it does mean that it loses whatever falsity it once had.Depending on the type you’re working with,you can convert it back to an unboxed value:

function(rhs){return!!this.valueOf()!==!!rhs};

6A Really Awesome Equality

There is something really important about Javascript that isn’t at all obvious from the way it’s used.It is this:The syntax foo.bar is,in all situations, identical to foo[’bar’].You could safely make this transformation to your code ahead of time,whether on value-properties,methods,or anything else. By extension,you can assign non-identi?er things to object properties:

16OK,I’m being biased about this point.I tend to treat Javascript more like Scheme than like Smalltalk,so I don’t think much in terms of classical object-oriented modeling.Also,since closures are really fast,it’s OK to use functional abstraction instead of inheritance.Javascript tends to be better suited to metaprogramming than inheritance.

17In some cases really slow.The di?erence between single-level and multiple-level prototype lookups in Firefox3.5,for instance,is enormous.

18!!x is just an idiom to make sure that x ends up being a boolean.It’s a double-negation,and !always returns either true or false.

15

var foo=[1,2,3];

foo[’@snorkel!’]=4;

foo[’@snorkel!’]//=>4

You can also read properties this way,of course:

[1,2,3][’length’]//=>3

[1,2,3][’push’]//=>[native function]

In fact,this is what the for(var...in...)syntax was built to do: Enumerate the properties of an object.So,for example:

var properties=[];

for(var k in document)properties.push(k);

properties//=>a boatload of strings

However,for...in has a dark side.It will do some very weird things when you start modifying prototypes.For example:

Object.prototype.foo=’bar’;

var properties=[];

for(var k in{})properties.push(k);

properties//=>[’foo’]

To get around this,you should do two things.First,never modify Object’s prototype,since everything is an instance of Object(including arrays and all other boxed things);and second,use hasOwnProperty:19

Object.prototype.foo=’bar’;

var properties=[],obj={};

for(var k in obj)obj.hasOwnProperty(k)&&properties.push(k); properties//=>[]

And very importantly,never use for...in to iterate through arrays(it returns string indices,not numbers,which can cause problems)or strings. Either of these will fail if you add methods to Array or String(or Object,but you shouldn’t do that).

7If You Have20Minutes...

Javascript can do almost anything that other languages can do.However,it might not be very obvious how to go about it.

19OK,so you’re probably wondering why we don’t see the hasOwnProperty method from a for ...in loop,since it’s obviously a property.The reason is that Javascript’s attributes have invisible ?ags(as de?ned by the ECMAScript standard),one of which is called DontEnum.If DontEnum is set for some attribute,then a for...in loop will not enumerate it.Javascript doesn’t provide a way to set the DontEnum?ag on anything you add to a prototype,so using hasOwnProperty is a good way to prevent looping over other people’s prototype extensions.Note that it fails sometimes on IE6;I believe it always returns false if the prototype supplies an attribute of the same name.

16

7.1Forwarding constructors

I mentioned earlier that constructors can’t be forwarded.That isn’t exactly true.You can use this egregious hack(which is actually kind of beautiful in a don’t-try-this-at-home sort of way):20

var ctor=...;//we want to wrap this

var wrapper=function(){

var params=/?function\s*\(([?)]*)\)/.exec(ctor.toString());

if(params){

var unlikely=’_’+(Math.random()<<20).toString(36);

eval(’var’+unlikely+’=ctor;’);

var f=eval(//fails on IE6’function(’+params[1]+’){’+

’return new’+unlikely+’(’+params[1]+’);’+’}’);

return f.apply(this,arguments);

}

};

Now you can say wrapper(x,y,...)and it will be translated into new ctor(x,y,...)–and because wrapper is just a regular function,you can call its apply method to give it an array of arguments.

7.2Iterators for cool people

Because languages like Ruby showed the world just how pass′e for loops really are,a lot of self-respecting functional programmers don’t like to use them.If you’re on Firefox,you won’t have to;the Array prototype includes map and forEach functions already.But if you’re writing cross-browser code and aren’t using a library that provides them for you,here is a good way to implement them:

Array.prototype.each=Array.prototype.forEach||function(f){ for(var i=0,l=this.length;i

f(this[i]);

return this;//convenient for chaining

};

Array.prototype.map=Array.prototype.map||function(f){ var ys=[];

for(var i=0,l=this.length;i

ys.push(f(this[i]));

return ys;

};

20In general,anytime you run into something that’s“impossible”in Javascript,you have a technique hack at your disposal.The sledgehammer is eval,which lets you do anything.

17

As far as I know this is(almost)the fastest way to write these functions.We declare two variables up-front(i and l)so that the length is cached;Javascript won’t know that this.length is invariant with the for loop,so it will check it every time if we fail to cache it.This is expensive because due to boxing we’d have a failed hash-lookup on this that then dropped down to this.__proto__, where it would?nd the special property length.Then,a method call would happen to retrieve length.21

The only further optimization that could be made is to go through the array backwards(which only works for each,since map is assumed to preserve order): Array.prototype.each=function(f){

for(var i=this.length-1;i>=0;--i)

f(this[i]);

};

This ends up being very slightly faster than the?rst implementation because it changes a?oating-point subtraction(required to evaluate=.

You can also de?ne an iterator for objects,but not like this:

//NO NO NO!!!Don’t do it this way!

Object.prototype.each=function(f){

for(var k in this)this.hasOwnProperty(k)&&f(k);

};

Much better is to implement a separate keys function to avoid polluting the Object prototype:

var keys=function(o){

var xs=[];

for(var k in o)o.hasOwnProperty(k)&&xs.push(k);

return xs;

};

7.3Java classes and interfaces

No sane person would ever want to use these.But if you’re insane or are being forced to,then the Google Web Toolkit will give you a way to shoot yourself in the foot and turn it into Javascript.

21This gets into how Javascript presents certain APIs.Internally it has a notion of gettable and settable properties,though there isn’t a cross-browser way to create them.But properties such as length,childNodes,etc.are all really method calls and not?eld lookups.(Try assigning to one and you’ll see.)

18

7.4Recursive metaclasses

There are di?erent ways to approach this,but a straightforward way is to do something like this:22

var metaclass={methods:{

add_to:function(o){

var t=this;

keys(this.methods).each(function(k){

o[k]=bind(t.methods[k],o);//can’t use/this/here });

return o}}};

metaclass.methods.add_to.call(metaclass,metaclass);

At this point,metaclass is now itself a metaclass.We can start to imple-ment instances of it:

var regular_class=metaclass.add_to({methods:{}});

regular_class.methods.def=function(name,value){

this.methods[name]=value;

return this;

};

regular_class.methods.init=function(o){

var instance=o||{methods:{}};

this.methods.init&&this.methods.init.call(instance);

return this.add_to(instance);

};

regular_class.add_to(regular_class);

This is a Ruby-style class where you can de?ne public methods and a con-structor.So,for example:

var point=regular_class.init();

point.def(’init’,function(){this.x=this.y=0});

point.def(’distance’,function(){

return Math.sqrt(this.x*this.x+this.y*this.y)});

We’re using the rather verbose this.x,which may o?end some Python-eschewing Rubyists.Fortunately,we can use dynamic rewriting to use the$ where Rubyists would use@:23

var ruby=function(f){

return eval(f.toString().replace(/\$(\w+)/g,

function(_,name){return’this.’+name}));

22Remember that a class is just a function that produces instances.Nothing about the new keyword is necessary to write object-oriented code(thank goodness).

23And,in fact,we could bake this ruby()transformation into a metaclass to make it totally transparent if we wanted to.

19

};

point.def(’init’,ruby(function(){$x=$y=0}));

point.def(’distance’,ruby(function(){

return Math.sqrt($x*$x+$y*$y)}));

And now you can use that class:

var p=point.init();

p.x=3,p.y=4;

p.distance()//=>5

The advantage of using metaclasses is that you can do fun stu?with their structure.For example,suppose that we want to insert method tracing into all of our points for debugging purposes:24

keys(point.methods).each(function(k){

var original=point.methods[k];

point.methods[k]=function(){

trace(’Calling method’+k+’with arguments’+

Array.prototype.join.call(arguments,’,’));

return original.apply(this,arguments);

};

});

Now trace(which isn’t a Javascript built-in,so you’d have to de?ne it) would be called each time any method of a point instance was called,and it would have access to both the arguments and the state.

7.5Tail calls and delimited continuations

Javascript does not do tail-call optimization by default,which is a shame be-cause some browsers have short call stacks(the shortest I’m aware of is500 frames,which goes by especially quickly when you have bound functions and iterators).Luckily,encoding tail calls in Javascript is actually really simple: Function.prototype.tail=function(){return[this,arguments]}; Function.prototype.call_with_tco=function(){

var c=[this,arguments];

var escape=arguments[arguments.length-1];

while(c[0]!==escape)

c=c[0].apply(this,c[1]);

return escape.apply(this,c[1]);

};

24The example here used to contain the expression arguments.join,which is invalid–arguments isn’t an array.Now it uses the“pretend this is an array for the purposes of calling join on it”idiom, which usually works.(Though you’ll sometimes get errors about methods not being generalized, as is the case on Chrome if you try to use Array.prototype.toString()this way.)

20

JavaScript入门教程(初学者不可多得的优秀入门教材,通俗易懂,专业术语通俗化)

第 1 章 JavaScript 语言入门 1 为什么学习 JavaScript
提要:Javascript 是学习脚本语言的首选。她兼容性好,绝大多数浏览器均支持 Javascript,而且她功能强大,实现简单方便,入门简单,即使是程序设计新手也可以非常 快速容易地使用 JavaScript 进行简单的编程。
Javascript 是由 Netscape 公司创造的一种脚本语言。为便于推广,被定为 javascript,但 是 javascript 与 java 是两门不相干的语言, 作用也不一样。 作为一门独立的编程语言, javascript 可以做很多的事情,但它最主流的应用还是在 Web 上——创建动态网页(即网页特效)。 Javascript 在网络上应用广泛, 几乎所有的动态网页里都能找到它的身影。 目前流行的 AJAX 也是依赖于 Javascript 而存在的。 Javascript 与 Jscript 也不是一门相同的语言, Jscript 和 vbscript 是微软开发的两种脚本语 言,微软,Netscape 公司以及其他语言开发商为减少 web 开发者的兼容麻烦,所以成立 ECMA , 该组 织 专 门制定 脚 本 语 言的 标 准 和规范 。 ECMA 制 定 的标 准脚 本 语 言 叫做 ECMAScript,Javascript 符合 ECMA 的标准,其实 Javascript 也可以叫做 ECMAScript. Jscript 也 ECMA 的标准, 但用户较少。vbscript 仅局限在微软的用户, Netscape 不支持。 概括地说,JavaScript 就是一种基于对象和事件驱动,并具有安全性能的脚本语言,脚 本语言简单理解就是在客户端的浏览器就可以互动响应处理程序的语言, 而不需要服务器的 处理和响应,当然 JavaScript 也可以做到与服务器的交互响应,而且功能也很强大。而相对 的服务器语言像 asp https://www.360docs.net/doc/326680318.html, php jsp 等需要将命令上传服务器,由服务器处理后回传处理结 果。对象和事件是 JavaScript 的两个核心。 JavaScript 可以被嵌入到 HTML 文件中,不需要经过 Web 服务器就可以对用户操作作 出响应,使网页更好地与用户交互;在利用客户端个人电脑性能资源的同时,适当减小服务 器端的压力,并减少用户等待时间。
2 将 JavaScript 插入网页的方法
与在网页中插入 CSS 的方式相似,使用
language="javascript"表示使用 JavaScript 脚本语言,脚本语言还有 vbscript、 jsscript 等,如果没有 language 属性,表示默认使用 JavaScript 脚本。其中的...就是代 码的内容。例如:

股市高手速成班培训讲义第四

股市高手速成班培训讲义 第四课新股中线黄金买入法 在前面二节里,我们分别学习了“底部中线黄金买入法”和“顶部中线黄金买入法”。现在,。底部中线黄金买入法和顶部中线黄金买入法,二者最大的区别是什么? 它们最大的区别是:一个是“底”字,一个是“顶”字。 对于底部中线黄金买入法和顶部中线黄金买入法的区别,我可用二个简单的箭头来形容非常形象。其中箭头实体代表30日线,其中含义大家自己去体会。 今天我要讲的,是新股中线黄金买入法 在每一轮新行情启动之时,主力常常启动新股作为行情的开路先锋,以起到刺激人气活跃市场的作用。可以说,每一轮新的大牛市行情中,新股都成为中线大牛股的摇篮。因此,一旦我们把握住新股的中线机会,收益往往非常可观。 在介绍新股中线黄金买入法之前,我想花五分钟的时间,请大家仔细看看下面几只股票。并且请大家在边看这些股票的时候,边思考一个问题:我们如何判断这些新股上市后会中线大涨? 先看中国中铁(601390)上市之初的表现; 再看西部矿业(601168)上市之初的表现; 再看怡亚通(002183)上市之初的表现; 再看全聚德(002186)上市之初的表现; 在讲方法之前,再请大家花五分钟时间看看下面这些图。 新股中线黄金买入法: 方式1、短线洗盘即拉升

新股中线黄金买入法: 方式1、短线洗盘即拉升 特征:1、新股首日收涨幅不超过10%的阳线,或者涨跌幅均不大的小阴小阳线; 2、上市后出现回落或阴阳十字星式洗盘走势; 3、在短期内(不超过3周)再次放量上涨突破上市首日最高点,或创上市后新高; 结论1:在股价放量突破首日最高点时,就是新股中线黄金买入点。 原理:新股上市首日换手巨大,既有主力大规模建仓资金,也有许多短线跟风资金。由于跟风盘较多,主力常从上市次日开始洗盘;经过短暂洗盘后,某日突然放量拉升突破上市首日最高点。既然主力愿意在短时间内解放上市首日巨额套牢盘,说明主力展开新行情的意愿非常强烈,则该股中线上涨的可能性非常大。 强化记忆:花五分钟时间体会这些股票上市之初的走势: 先看西部矿业(601168) 再看工商银行(601398)

VB-条件语句-循环语句练习题

VB条件语句和循环语句 测试习题(满分100分) 班级姓名 一、根据程序写运行结果 1.写出下列程序的运行结果。(5分) Private sub command1_click X=VAL(text1.text) IF X<0 THEN Y=ABS(X) ELSE Y= -X END IF PRINT "Y=";Y End sub 运行结果: (1)在text1中输入99 输出 (2)在text1中输入-23 输出 2. 写出下列程序的运行结果。(5分) Private sub command1_click N=1 FOR X=3 TO 10 STEP 3 N=N*2 NEXT X PRINT "N=";N End sub 运行结果: 3.写出下列程序的运行结果。(5分) Private sub command1_click S=0 FOR X=10 TO 1 STEP 4 S=S+X NEXT X PRINT " S=";S End sub(5分) 运行结果: 4.写出下列程序运行结果。 (5分) Private sub command1_click FOR I= 1 TO 5 step 2 PRINT I, NEXT I End sub 运行结果: 5. 写出下列程序的运行结果。(5分) Private sub command1_click X=VAL(text1.text) IF X/2=Int(X/2) THEN S=X+1 ELSE S=X-1 END IF PRINT "S=";S End sub 若在text1中输入以下数字,运行结果: (1)99 (2)98 二、根据题意,完善下列程序。(每空5分) 1.请设计一个程序,将从键盘上任意输入的两个数中最大的那个选出来。 Private sub command1_click A=VAL(text1.text) B=VAL(text2.text) IF A>B THEN MAX=________ ELSE MAX= ENDIF PRINT "MAX=";MAX End sub 2.求和S=1+3+5+7+…+99 Private sub command1_click S=0 FOR I=1 TO 99 STEP S=________ NEXT I PRINT “S=”;S End sub 3.完善下列程序,使其能求出 2+4+6+……+100之和。 Private sub command1_click FOR I =____ TO 100 STEP _____ S= S+I ______ I PRINT S End sub

Javascript基础教程

Javascript简介 (2) Javascript简介 (2) 简单的Javascript入门示例 (4) 编写Javascript 代码 (5) 语句(Statements) (5) 语句块(Blocks) (6) 注释(Comments) (7) 表达式(Expressions) (8) 赋值和等于(Assignments and Equality) (9) Javascript常用运算符(Operators) (10) 算术运算符 (10) 逻辑运算符 (11) 赋值运算符 (12) Javascript 循环语句(Javascript Loop Statements) (12) 使用for 循环语句 (13) 使用for...in 循环语句 . (15) 使用while 和do...while 循环语句 (17) 使用break 和continue 语句 (20) Javascript写在哪里 (23) Javascript在之间 (23)

Javascript在之间 (24) Javascript放在外部文件里 (25) Javascript变量(Javascript Variables) (26) 什么是变量? (26) 变量的声明(Declaring Variables) (26) 变量的命名规则 (27) Javascript条件语句(Javascript Conditional Statements) (27) 单项条件结构(if条件语句) (28) 双向条件结构(if...else条件语句) (29) 多项条件结构(switch条件语句) (31) Javascript保留字(Javascript Reserved Words) (32) Javascript未来保留字(Javascript Future Reserved Words) (33) Javascript简介 Javascript简介

汉语口语速成(基础篇)

《汉语口语速成:基础篇(上、下)》教学大纲 一、课程性质与教学对象 本课程专为短期来华留学生开设,以培养学生汉语交际技能为目的,是短期汉语强化教学模式的主干课程之一,是以加强学生汉语口语表达能力和实际交际能力为目标的专项技能培训课。通过优选与学生的日常生活、学习、交际等方面的活动有直接联系的话题、情景和语言要素进行教学,围绕这些话题和情景设定一系列典型的语言交际任务,让学生在课堂上完成。《基础篇》适合具有初步听说能力,掌握汉语简单句型和800个左右词汇的学习者学习。两册共25课,涉及大纲中以乙级和丙级词汇为主的常用词、汉语特殊句式、复句以及日常生活、学习、社交等交际活动的简单交际项目。 本课程的总学时为72学时,周学时为4,每学时35分钟,共十八周。 二、课程教学目的 通过一学期的教学,使学生高效地完成初级阶段相关语言要素(汉字、语音、词汇、语法)和交际任务项目的学习,同时了解一定的文化背景知识,迅速提高汉语语言能力和语言交际能力,能够用汉语进行日常生活和学习范围内的基本的口头交际活动,并能进行简单社会交际活动。 三、教学基本内容 第一课认识一下 一、注释 1、去法国的中国人越来越多,…… 2、是公司派我来学习的。 3、我要先在这儿学习半年,然后在中国工作。 4、我要一边学汉语,一边学京剧。 5、我觉得汉语一点也不难。 5、美国学生爱珍学汉语是为了学京剧。 二、课文:介绍学习汉语的目的 三、会话操练 四、练习 第二课吃点什么? 一、注释

1、这家饭馆儿的饭菜又好吃又便宜。 2、我一看汉语菜单就头疼。 3、挺好吃的。 4、你们吃得了吗? 5、越辣我越喜欢 6、就是有一点点咸 二、课文:饭菜的味道怎么样 三、会话操练 四、练习 第三课在校园里 一、注释 1、你这是去哪儿? 2、原来是这样,怪不得这几天我经常看见你在校园里转。 3、我发现校园里书店、洗衣店、理发店……什么都有,非常方便。 4、想不到你已经这么熟悉了! 5、里边不但有吃的、喝的,而且有穿的、用的、东西很全。 6、他们努力学习,为的是以后能找到一个好工作…… 二、课文:校园生活的交谈 三、会话操练 四、练习 第四课住的麻烦 一、注释 1、才来一个星期,你跟服务员就很熟了。 2、下午我学习的时候,他偏偏要睡觉! 3、她既没说同意,也没说不同意…… 4、只说要等等。 5、天天睡不好,怎么能有精神? 6、咱们的宿舍楼不是临街吗? 7、你或者想办法换个房间,或者快点儿习惯新环境。 8、再说吧。 二、课文:住宿的问题 三、会话操练 四、练习 第五课怎么去好? 一、注释 1、有名的地方差不多都去过了…… 2、香山比天坛近一些…… 3、这几天我天天坐,已经坐腻了。 4、要是累了,咱们就停下来歇一会儿。 5、明天路上千万别堵车。 6、那是因为故宫在市区,沿途都是热闹的地方,所以很容易堵车。

条件语句、循环语句

1.2.2-1.2.3条件语句和循环语句(第二、三课时) 教学目标: 知识与技能 (1)正确理解条件语句和循环语句的概念,并掌握其结构的区别与联系。 (2)会应用条件语句和循环语句编写程序。 过程与方法 经历对现实生活情境的探究,认识到应用计算机解决数学问题方便简捷,促进发展学生逻辑思维能力 情感态度与价值观 了解条件语句在程序中起判断转折作用,在解决实际问题中起决定作用。深刻体会到循环语句在解决大量重复问题中起重要作用。减少大量繁琐的计算。通过本小节内容的学习,有益于我们养成严谨的数学思维以及正确处理问题的能力。 重点与难点 重点:条件语句和循环语句的步骤、结构及功能。 难点:会编写程序中的条件语句和循环语句。 学法与教学用具 计算机、图形计算器 教学设想 【创设情境】 试求自然数1+2+3+……+99+100的和。

显然大家都能准确地口算出它的答案:5050。而能不能将这项计算工作交给计算机来完成呢?而要编程,以我们前面所学的输入、输出语句和赋值语句还不能满足“我们日益增长的物质需要”,因此,还需要进一步学习基本算法语句中的另外两种:条件语句和循环语句。 【探究新知】 (一)条件语句 算法中的条件结构是由条件语句来表达的,是处理条件分支逻辑结构的算法语句。它的一般格式是:(IF -THEN -ELSE 格式) 当计算机执行上述语句时,首先对IF 后的条件进行判断,如果条件符合,就执行THEN 后的语句1,否则执行ELSE 后的

语句2。其对应的程序框图为:(如上右图) 在某些情况下,也可以只使用IF -THEN 语句:(即 IF -THEN 格式) 计算机执行这种形式的条件语句时,也是首先对IF 后的条件进行判断,如果条件符合,就执行THEN 后的语句,如果条件不符合,则直接结束该条件语句,转而执行其他语句。其对应的程序框图为:(如上右图) 条件语句的作用:在程序执行过程中,根据判断是否满足约定的条件而决定是否需要转换到何处去。需要计算机按条件进行分析、比较、判断,并按判断后的不同情况进行不同的处理。 【例题精析】 〖例1〗:编写程序,输入一元二次方程20ax bx c ++=的系数,输 IF 条件 THEN 语句 END IF

javascript基础练习题

基础练习题 一、简单Java程序调试 1)以下哪个是Java应用程序main方法的有效定义? A. public static void main(); B. public static void main( String args ); C. public static void main( String args[] ); D. public static void main( Graphics g ); E. public static boolean main( String a[] ); 2) 编译和运行以下代码的结果为: public class MyMain{ public static void main(String argv){ System.out.println("Hello cruel world"); } } A.编译错误; B.运行输出"Hello cruel world"; C.编译无错,但运行时指示没有定义构造方法。 D.编译无错,但运行时指示没有正确定义main方法。 3)下列选项中不属于Java虚拟机的执行特点的一项是: A.异常处理B.多线程C.动态链接D.简单易学 4)不属于Java语言特点的一项是: A.分布式 B. 安全性 C. 编译执行 D.面向对象 5)以下程序的运行结果为: public class Test{ public static void main(String argv[ ]){ System.out.println("x="+5); } } A. 5 B. x=5 C. "x="+5 D. "x="5 6) 以下程序的运行结果为: public class Test{ public static void main(String argv[ ]){ System.out.println("good"+"morning"); } } A. goodmorning B. "good"+"morning" C. good morning D. good+morning 二、Java符号与表达式 1) 现有一个int类型的整数和一个double类型的数进行加法运算,则得到的结果类型为: A.int类型 B. double类型 C. float类型 D. long类型 2)下面程序段的输出结果是:

汉语口语速成 提高篇,语法, 整理1-10课

第一课 1、算:可以说是,可以这样认为---????-----????????. 在古北,三室一厅一个月九千块算便宜的。 今天15度,不算冷。 在我们部门,她算漂亮的。 2、尽管:随便,放心地做???+??/??(????) 今天我请客,你们尽管点。 有什么问题,你尽管和我说。 大小不合适,尽管告诉我。 3、来着:用在句尾,对以前提到的事情忘了或不太确定进行提问---??? 你叫什么来着? 老板刚刚说什么来着? 4、一下子:表示一次动作或很短的时间???/ ?????? 这个电饭锅很便宜,我一下子买了2个。 他2天没吃饭了,恨不得一下子把这么多好吃的全吃了。 5、并:并+不/没等否定词,强调实际情况和看到的、想象的不一样??,????? 别看他住别墅、开宝马,其实并不是有钱人。 韩国语看起来很简单,但是学起来并不容易。 6、对了:用在句子的开头表示突然想起来什么事情,或者作插入语表示转移话题。?? (老公要出门了)对了,我的手机没带,帮我找一下。 我女儿今年6岁了,对了,你孩子几岁了? 第二课 1、下来 从高到低?????????? 我在楼下等你,你快下来吧 动作持续到说话的时间或者过去的某一时刻 ?????????????????? 她已经尝试过好几次要减肥,可是每次都坚持不下来。 2、下去

从高到低?????????? 你男朋友在1楼等你,你快下去吧! 动作持续到将来某一时刻 为了能穿上这件衣服,不管减肥有多难,我都要坚持下去。 3、别提多……了???----- ?/???? 感冒又发烧,别提多难受了。 女儿6岁生日的时候,我们带她去迪士尼乐园,那天她别提多高兴了。 4、非……不可(?/ ???...???) 要想说一口流利的汉语,非学好发音不可。 这么冷的天,你穿这么少出去,非感冒不可。 酒后开车,非出事不可。 5、至于常用否定形式是“不至于”或者是反问句,表示事情没有严重到某个程度 ?????????? A:今天早上的报纸说:一对刚结婚没多久的小两口去商场买东西,因为意见不合,两个人要离婚了。 B:为这么点儿小事就离婚?不至于吧? 他怎么还没来?开车不至于这么慢吧? 转移到新的话题???????? 新的项目对公司来说非常重要,小王你负责这部分,小李你负责那部分,至于小张,你帮我整理一下这些材料吧。 今年的年假我打算先回家陪爸爸妈妈,至于旅游,明年再说吧。 6、千万+别/要/不要??---????. --??? 明天总部有人来检查,你千万别迟到。 公司规定周一到周五要穿正装上班,你千万不要穿牛仔裤、运动鞋。 过马路的时候千万要小心。 我有件事情想和你说,你千万别生气。 第三课 1、V惯/V不惯?????/??? 爸爸喝惯了白酒,喝不惯红酒。

让你十分钟看懂中国

让你十分钟看懂中国 中国到底什么样? 中华人民共和国,简称中国。位于东亚,与14个国家或地区接壤,是世界人口最多的国家,有13亿人口,而世界人口是68亿。也就是说,世界人每5个人中就有一个是中国人。中国有56个民族,汉族占总人口的92%。由中国共产党一党执政,执政地位被写入了宪法中。中国奉行无神论,其宗教文化扎根于儒教、佛教和道教,社会道德也多根源于此。中国也有大量的穆斯林,沿丝绸之路到达中国,保持着自己的传统。历史 中国是文明古国之一,有五千年文明,世袭的封建王朝终结于1912年。中国古代有许多重要的科技发明,包括印刷术、造纸术、火药和指南针,也有很多著名的建筑,比如绵延四千英里的长城,相当于往返伦敦和巴黎30次的距离。抗战和内战结束后,毛泽东领导共产党于1949年夺取了中国大陆的政权,建立了中华人民共和国。中国历史和未来塑造了当今的中国,许多城市很像西方的大城市,但二者岑仔这巨大的文化差异,走出城市就能看到不断增大的城乡贫富差距。 语言 汉语是世界上使用最广泛,同事让外国人最难搞懂的语言之一。汉语的放盐很多,使用最多的是普通话和粤语。汉子发源于象形文字,有超过四万个汉字,受过良好教育的人能认识大约六千个字,阅读报纸需要认识三千字左右。 农历 中国用十二生肖来代表年份,今年是兔年。农历新年是最重要的传统节日,门窗会贴上红色的剪纸和对联,象征幸福、富裕和长寿。除夕夜家人会一起吃年夜饭,过年是会放鞭炮。大年初一,小孩子会给长辈拜年,长辈则会发红包。中国人喜欢红色,红色象征着繁荣和吉利。最大规模的人类迁徙也发生于春节期间,1.5亿到两亿农民工会带回一年的收入,和家人一起吃团圆饭。 青年 大多数夫妻都需要遵守一胎化政策,这是1979年制定的控制人口增长的政策。据信,该政策避免了3亿人口的出生,政策的另一个结果是早就了一代“小皇帝”,以自我为中心的一代人。中国的教育在快速发展,但仍然以应试为主,课程学习都已考试为中心。中国的学生能记住大量的只是和信息,但却缺乏批判思维、独立思考和创新的能力。由于更加偏爱男孩,中国失衡的男女性别比达到了120比100,到2020年,男性将比女性多三千万。 经济 自1978年改革开放以来,中国经济增长了90倍,是增长最快的主要经济体。预测2011年到2015年,平均GDP增长会保持9.5%。中国是世界最大的出口国,第二大进口国,也是世界第二大奢侈品消费国。目前的GDP位于世界第二,大约6万亿美元,相当于美国的四成,但人均只有4300美元,排名一百位开外。不同地区、城乡之间发展也不均衡,发达地区集中在东南沿海,其余地方则普遍落后。过去十年,城市以年均10%的速度狂涨,这是人类历史上前所未有的规模。中国喜欢用摩天大楼来显示经济实力,现在有超过200幢在建,未来三年,每五天就会有一桩摩天大楼竣工,五年内总共达到800幢,比美国多四倍。中国是世界工厂,每三件家电、三件玩具、两双鞋子、两件衣服中,就有意见是由中国制造的。中国是世界最大的能源消费国,但七成能源由煤炭提供。环保人事警告说,水污染是中国最严重的环境问题,但2009年中国就向清洁能源投资了346亿美元,成为世界最大的可再生能源投资国,是风力发电机和太阳能电池板最大的生产国。 饮食

6、条件语句和循环语句

6、条件语句和循环语句 学习目标 1.正确理解条件语句和循环语句的概念,并掌握其结构的区别与联系。 2.会应用条件语句和循环语句编写程序。 3.培养学生形成严谨的数学思维以及正确处理问题的能力。 学习过程 一、课前准备 复习:回顾三种基本算法语句。 引入:顺序结构的框图可以用输入语句,输出语句,赋值语句来表示,条件结构、循环结构的语句要转化成计算机理解的语言,我们必须学习条件语句、循环语句. 二、新课导学 探究:条件语句和循环语句 (一)条件语句 条件语句的一般格式是: . 当计算机执行上述语句时,首先对IF 后的条件进行判断,如果条件符合,就执行THEN 后的语句1,否则执行ELSE 后的语句2。 在某些情况下,也可以只使用 IF-THEN 语句:(即 ) 计算机执行这种形式的条件语句时,也是首先对IF 后的条件进行判断,如果条件符合,就执行THEN 后的语句,如果条件不符合,则直接结束该条件语句,转而执行其他语句。 (二)循环语句 满足条件? 语句1 语句2 是 否 IF 条件 THEN 语句1 ELSE 语句2 END IF IF 条件 THEN 语句 END IF

算法中的循环结构是由循环语句来实现的。对应于程序框图中的两种循环结构,一般程序设计语言中也有 和 两种语句结构。即WHILE 语句和UNTIL 语句。 (1)WHILE 语句的一般格式是: (2)UNTIL 语句的一般格式是: 思考:你觉得WHILE 型语句与UNTIL 型语句之间有什么区别呢? 三、典型例题 例1 编写程序,输入一元二次方程2 0ax bx c ++=的系数,输出它的实数根。 例2 编写程序,计算自然数1+2+3+……+99+100的和。 WHILE 条件 循环体 WEND 满足条件? 循环体 是 否 DO 循环体 LOOP UNTIL 条件

10分钟了解现货黄金

10分钟了解现货黄金

10分钟让您了解国际现货黄金 ★什么叫现货黄金? 现货黄金又叫伦敦金,因最早起源于伦敦而得名。伦敦金通常被称为欧式黄金交易。以伦敦黄金交易市场和苏黎世黄金市场为代表。投资者的买卖交易记录只在个人预先开立的“黄金存折账户”上体现,而不必进行实物金的提取,这样就省去了黄金的运输、保管、检验、鉴定等步骤,其买入价与卖出价之间的差额要小于实金买卖的差价。这类黄金交易没有一个固定的场所。在伦敦黄金市场整个市场是由各大金商、下属公司间的相互联系组成,通过金商与客户之间的电话、电传等进行交易;在苏黎世黄金市场,则由三大银行为客户代为买卖并负责结账清算。伦敦的五大金商(罗富齐、金宝利、万达基、万加达、美思太平洋)和苏黎士的三大银行(瑞士银行、瑞士信贷银行和瑞士联合银行)等都在世界上享有良好的声誉,交易者的信心也就建立于此。 ★现货黄金优点? 黄金的价值是自身所固有的和内在的“全球硬通货币”,并且有千年不朽的稳定性,所以无论天灾人祸,黄金的价值永恒。黄金是与货币密切相关的金融资产,因此很容易变现。更由于黄金24小时的交易市场,因此随时可以变钞票。黄金具有世界价格,还可以根据兑换比价,兑换为其他国家货币。 一、金价波动大:根据国际黄金市场行情,按照国际惯例进行报价。因受国际上各种政治、经济因素如a.美元b.石油c.央行储备d.战争风险,以及各种突发事件的影响,金价经常处于剧烈的波动之中。可以利用这差价进行实盘黄金买卖。 二、交易服务时间长:每天为二十四个小时交易,涵盖主要国际黄金市场交易时间。 三、资金结算时间短:当日可进行多次开仓平仓,提供更多投资机遇。 四、操作简单:有无基础均可,即看即会;比炒股更简单,不用选股多么麻烦,分析判断相对简单,跟美圆,原油走势紧密相关。全世界都在炒这种黄金,每天交易大约20万亿美金。一般庄家无法兴风作浪。在这个市场靠的只有自己的技术。 五、赚的多:黄金涨,您做多,赚;黄金跌,你做空,也赚!(股票涨才赚,跌则亏)双向交易,真正的上涨下跌都赚钱。 六、趋势好:炒黄金在国内才刚刚兴起,股票、房地产、外汇等在刚开始是都赚疯了,黄金也不例外。而且双向更灵活. 七、保值强:黄金从古至今都是最佳保值产品这之一,升值潜力大;现在世界上通货膨胀加剧,将推进黄金增值。 ☆现货黄金的特点: 1.T+0交易规则:允许当日平仓,并允许投资者进行多次交易。 2.资金杠杆:通过定金模式下单,提高资金利用率,降低交易门槛。 3.报价:直接以美国道琼斯即时报价系统为交易依据,以美元/盎司标价,快速直观。 4.交易时间:24小时交易时段,涵盖了交易量最大的欧洲盘时段和美洲盘时段,增加获利机会。交易时间宽松,交易方式便利,不与工作时间、地点相冲突,尤其适合上班一族。 6.交易方式:交易快捷方便,简单易学。客户主要采用网上交易系统下单委托,也可以电话委托下单,公司另外提供行情分析系统,短信报价平台。 7.市场公开:国际现货黄金市场向全球公开,透明度高,每日交易量接近2万亿美元,很难出现庄家。价格走势受人为影响小,可分析性强,隔夜风险不大。

5条件语句和循环语句

第二、三课时 1.2.2-1.2.3条件语句和循环语句 教学目标: 知识与技能 (1)正确理解条件语句和循环语句的概念,并掌握其结构的区别与联系。 (2)会应用条件语句和循环语句编写程序。 过程与方法 经历对现实生活情境的探究,认识到应用计算机解决数学问题方便简捷,促进发展学生逻辑思维能力 情感态度与价值观 了解条件语句在程序中起判断转折作用,在解决实际问题中起决定作用。深刻体会到循环语句在解决大量重复问题中起重要作用。减少大量繁琐的计算。通过本小节内容的学习,有益于我们养成严谨的数学思维以及正确处理问题的能力。 重点与难点 重点:条件语句和循环语句的步骤、结构及功能。 难点:会编写程序中的条件语句和循环语句。 学法与教学用具 计算机、图形计算器 教学设想

【创设情境】 试求自然数1+2+3+……+99+100的和。 显然大家都能准确地口算出它的答案:5050。而能不能将这项计算工作交给计算机来完成呢?而要编程,以我们前面所学的输入、输出语句和赋值语句还不能满足“我们日益增长的物质需要”,因此,还需要进一步学习基本算法语句中的另外两种:条件语句和循环语句(板出课题) 【探究新知】 (一)条件语句 算法中的条件结构是由条件语句来表达的,是处理条件分支逻辑结构的算法语句。它的一般格式是:(IF-THEN-ELSE 格式) 当计算机执行上述语句时,首先对IF 后的条件进行判断,如果条件符合,就执行THEN 后的语句1,否则执行ELSE 后的语句2。其对应的程序框图为:(如上右图)

在某些情况下,也可以只使用IF-THEN 语句:(即IF-THEN 格式) 计算机执行这种形式的条件语句时,也是首先对IF 后的条件进行判断,如果条件符合,就执行THEN 后的语句,如果条件不符合,则直接结束该条件语句,转而执行其他语句。其对应的程序框图为:(如上右图) 条件语句的作用:在程序执行过程中,根据判断是否满足约定的条件而决定是否需要转换到何处去。需要计算机按条件进行分析、比较、判断,并按判断后的不同情况进行不同的处理。 【例题精析】 〖例1〗:编写程序,输入一元二次方程20ax bx c ++=的系数,输出它的实数根。 分析:先把解决问题的思路用程序框图表示出来,然后再根据程序框图给 出的算法步骤,逐步把算法用对应的程序语句表达出来。 IF 条件 THEN 语句

最新JavaScript_深度剖析(从入门到精通)

第一讲JavaScript语言概况 第二讲JavaScript基本数据结构 第三讲JavaScript程序构成 第四讲基于对象的JavaScript语言 第五讲创建新对象 第六讲使用内部对象系统 第七讲窗口及输入输出 第八讲WEB页面信息的交互 第九讲实现更复杂的交互 第一讲JavaScript语言概况 Internet时代,造就了我们新的工作和生活方式,其互联性、开放性和共享信息的模式,打破了传统信息传播方式的重重壁垒,为我们带来了新的机遇。随着计算机和信息时代的到来,人类社会前进的脚步在逐渐加快,每一天都有新的事情发生,每一天都在创造着奇迹。随着Internet技术的突飞猛进,各行各业都在加入Internet的行业中来。无论从管理方面,还是从商业角度来看,Internet都可以带来无限生机。通过Internet,可以实现地区、集体乃至个人的连接,从而达到一种“统一的和谐”。那么怎样把自己的或公司的信息资源加入到WWW 服务器,是广大用户日益关心的问题。采用超链技术(超文本和超媒体技术)是实现这个目标最简单的、最快速的手段和途径。具体实现这种手段的支持环境,那就是HTML 超文本标识语言。通过它们可制作所需的Web网页。 通过超文本(Hyper Text)和超媒体(Hyper Media)技术结合超链接(Hyper link)的链接功能将各种信息组织成网络结构(web),构成网络文档(Document),实现Internet上的“漫游”。通过HTML符号的描述就可以实现文字、表格、声音、图像、动画等多媒体信息的检索。 然而采用这种超链技术存在有一定的缺陷,那就是它只能提供一种静态的信息资源,缺少动态的客户端与服务器端的交互。虽然可通过CGI (Common Gateway Interface)通用网关接口实现一定的交互,但由于该方法编程较为复杂,因而在一段时间防碍了Internet技术的发展。而JavaScript的出现,无凝为Internet 网上用户带来了一线生机。可以这样说,JavaScript的出现是时代的需求,是当今的信息时代造就了JavaScript。 JavaScript的出现,它可以使得信息和用户之间不仅只是一种显示和浏览的关系,而是实现了一种实时的、动态的、可交式的表达能力。从而基于CGI静态的HTML页面将被可提供动态实时信息,并对客户操作进行反应的Web页面的取代。JavaScript脚本正是满足这种需求而产生的语言。它深受广泛用户的喜爱的

自我介绍一分钟

自我介绍一分钟 一段短短的自我介绍,其实是为了揭开更深入的面谈而设计的。一分钟的自我介绍,犹如商品广告,在短短60秒内,针对“客户”的需要,将自己最美好的一面,毫无保留地表现出来,不但要令对方留下深刻的印象,还要即时引发起“购买欲”。 自我认识若想一矢中的,首先必须知道你能带给公司什么好处。当然不能空口讲白话,必须有事实加以证明。最理想就是能够“展示”过去的成就。例如你曾为以往的公司设计网页,并得过奖项或赞扬。当然,这些例子都必须与现在公司的业务性质有关。职位愈高,自我认识就愈重要,应将个人的成败得失,尽录在日记中。这样,就可以时刻都清楚自己的弱点与强项。 投其所好清楚自己的强项后,便可以开始预备自我介绍的内容:包括工作模式、优点、技能,突出成就、专业知识、学术背景等。好处众多,但只有短短一分钟,所以一切还是与该公司有关的好。如果是一间电脑软件公司,应说些电脑软件的话题;如果是一间金融财务公司,便可跟他说钱的事,总之投其所好。但有一点必须紧记,话题所到之处,必须突出自己对该公司做出的贡献,如增加营业额、减低成本、发掘新市场等。 铺排次序内容的次序亦极重要,是否能紧握听众的注意力,全在于事件的编排方式。所以排在头位的,应是你最想他记得的事情。而这些事情,一般都是你最得意之作。与此同时,可呈上一些有关的作品或纪录增加印象分。 身体语言不管内容如何精采绝伦,若没有美丽的包装,还是不成的。所以

在自我介绍当中,必须留意自己在各方面的表现,尤其是声线。切忌以背诵朗读的口吻介绍自己。最好事前找些朋友作练习对象,尽量令声线听来流畅自然,充满自信。身体语言也是重要的一环,尤其是眼神接触。这不但令听众专心,也可表现自信。 曾有一项报告指出,日常的沟通,非语言性的占了70%。所以,若想面试成功,便应紧记注意一下你的身体语言。

股市高手速成班培训讲义第一至四课完整版

股市高手速成班培训讲义 第一课股市形态认识篇 在开始讲课之前,我想问大家一个问题。这个问题,我在周末免费试听的时候问过一些朋友,今天,我还想再问大家一次,大家需要大声地告诉我:你们这次来参加“股市高手速成训练班”的目的是什么?你们进入股市的最终目的又是什么? 刚才听了大家的回答,很好。我想多数人都很清楚地认识到,我们进入股市的目的,就是为了赚钱。我想没有人进入股市,是想体会亏损的痛苦;没有人进入股市,是想体会套牢的折磨。而我们参加这次股市专业化系统培训的最终目的,就是想跨越式地提高大家的实战操作水平,从而最终远离大幅亏损,从而最终保持稳健获利。所以,我们这次来参加培训的目,就是为了学技术,就是为了赚钱。 以上花费近几分钟的时间,和大家闲扯这些东西,我觉得是有必要的。大家也不要急,对于整个培训课程来说,这几分钟是非常短暂的,但我认为是非常必需的。因为,没有欲望的人是不可能成功的。 本堂课共分二部分内容:第一部分是股市运行基本规律认识;第二部分是快速看图训练 下面开始讲本堂课第一部分内容:如何深刻认识股票运行基本规律 这部分我要向大家介绍的内容,只是一张简单的图。大家现在不要说话,花五分钟的时间,先仔细地看看这张股票运行趋势图。 时间到。在开课的第一天,我准备一节课的时间,只让大家看一张图,这说明这张图在我心目中的崇高地位。 大家知道,真正高水平的职业投资者在波涛汹涌的股市中搏杀,其梦寐以求的就是在股市中寻找一套稳健获利的系统操盘技术,而学会实战看盘是整个投资获利的基础。因此如何以专

业的眼光、充分的技术依据来判断一只股票是否值得投资,就是这堂课要达到的初步目的。在上图中,A阶段是筑底阶段,C阶段是做头阶段,D阶段是下跌阶段。所有这三个阶段,股票提供的都只是短线机会,尤其是D阶段,绝对不容许做波段,甚至要干脆空仓休息,等待下一次机会来临。 心理回顾训练:现在大家不要说话,花二分钟,仔细回顾一下自己以前的股票操作中,获取较大利润的股票,当时是处于股票运行趋势图中的何种状态? 可能有人心里在犯嘀咕,这张图很简单啊,一眼就看明白了。但我要非常认真地提醒大家:绝对不要小看这张图。因为目前股市中任何一只股票,都必然处于这张“股市运行趋势图”中的某个阶段,没有任何一只股票可以例外。这是股市荣枯轮回的最根本的规律。这也是所有技术分析的理论基础,是职业投资者密不示人的操盘圣经。 这张图体现股市运行最根本的规律,大家务必牢记在心。这是我们迈向职业投资生涯中最最基础,但也是最最重要的一步,因此无论如何强调它的重要性都不过分。一旦大家真正读懂这张图,就会开始懂得市场语言,就可以很轻松地把握每只股票跳动的脉搏。 在上图中,股票运行趋势线为30日线。 在上图中,B阶段是上涨阶段,这是绝大多数做多动作展开的基础:中线操作、波段操作等等战术动作只有在上涨阶段才能展开。绝大多数的盈利必然是在股票运行在B阶段的过程中获得的,这一点,大家要从感性的角度上升到理论的高度。 股票运行趋势图剖析 1、如何理解筑底阶段的股票运行意义? 股票经过长期下跌以后,市场中的做空力量充分释放,一旦30日线即将走平,大家就要引起重视。这说明30天前买进该股票的投资者,已经处于保本状态,即将从亏损向盈利转变。如果这时5日、10日线与30日线横向交集在一起,说明这只股票短中线投资者的持仓成本基本一致,因此此时该股票的下跌动力已经基本丧失。 从流通的角度看,就是浮动筹码基本消失,套牢盘、割肉盘基本出局,多空力量基本平衡。此时盘面的显著特征就是成交量极度萎缩,交投极不活跃,成交量显示为地量水平。一般而言,如果这只股票已被主力相中的话,这个缩量横盘的过程,就可看成是主力的建仓过程。筑底完成的标志是:30日线由下跌走平开始转为翘头向上,同时伴随巨量突破。这说明主

JAVASCRIPT从入门到精通读书笔记

《JavaScript从入门到精通》读书笔记 今年的学习计划要读的书是《JavaScript权威指南》,里面内容比较有深度,所以决定买了比较好理解的《JavaScript从入门到精通》开始学习。 此书前半部分还是比较基础,平时工作中都能用得到,后半部分涉及到了Ajax、本地数据存储、离线应用和canvas图形等比较高级的用法。 首先主要介绍了JavaScript的发展历史版本变化,了解一下就可以。 初次使用JavaScript,重点讲了JavaScript的”字符串。例如。浏览器在加载如下代码就会产生一个错误。”); } Hi(); 错误原因:当浏览器解析到字符串””时,会结束JavaScript代码段的执行。解决方法: 使用转义字符把字符串‘’分成两部分来写就不会造成浏览器的误解。 代码测试和错误处理,理解浏览器的不同内核和代码不同的兼容性,在不同浏览器代码报错的时候会有不同的调试方法,学会使用浏览器的调试器对网页开发效率会有很大的提高。现在主流的浏览器是Chrome、Firefox、Safari等。 JavaScript的基本语法和各种变量,各种数据类型及各种数据类型的转换。 重点:避免变量污染 Var foo = function(){ Var a = 1, b = 2; Var bar = function(){ Var b = 3, c=4, //a= 1,b =3, c=4 a+=b + c; // a=8, b=3, c=4 }; //a=1, b=2, c = undefined bar(); //a= 21,b=2,c= undefined } JavaScript运算符的使用。JavaScript定义了51个运算符,主要分为一下几大类,位运算符、算术运算符、逻辑运算符、关系运算符、赋值运算符、对象炒作运算符和其他运算符。设计程序结构。程序都是由一个或多个语句组成的集合,语句表示一个可以执行的命令。用来完成特定的任务。大部分语句用于流程控制,在JavaScript中提供了if条件判断语句、switch多分枝语句、for循环语句、while循环语句、do/while循环语句、break语句、continue语句等7种流行控制语句。

如何画马,10分钟速成一匹栩栩如生的马

! STEP%1.%Mark!off!the!width!and!height!of!the picture.!Draw!two!ovals!for!the!head!and!body of!the!Arabian!horse.!Draw!a!line,!which!will act!as!the!center!of!its!head.STEP%2.%Draw!shapes!for!the!animal’s!body!and neck.!Add!a!smooth!guideline!for!its!tail.

STEP%3.%Draw!guidelines!to!define!places!for the!legs,!mouth,!eyes!and!nose!of!the!Arabian horse.STEP%4.%Sketch!the!legs,!hoofs,!ears!and!snout of!the!horse.

STEP%5.%Draw!the!eyes,!tail,!mane!and!hoofs!of the!Arabian!horse.!Detail!shapes!for!its!ears and!legs.STEP%6.%Work!on!the!whole!figure,!paying special!attention!to!detail.

STEP%7.%Draw!nostrils.!Contour!the!Arabian horse,!trying!to!vary!the!thickness!and darkness!of!the!line.!Add!more!detail!and!add the!ground.!Erase!all!guidelines.

相关文档
最新文档