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

更新时间:2023-06-06 18:34:08 阅读: 评论:0

Javascript in Ten Minutes
Spencer Tipping
considered
morningsideDecember22,2010
Contents
1Introduction3 2Types3 3Functions4
3.1Variadic behavior(a cool thing) (4)
augest
3.2Lazy scoping(a cool thing) (4)
3.3The meaning of this(the egregious disaster) (5)
3.3.1Important conquence: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.12Browr incompatibilities (13)
5Prototypes13
5.1Why new is awful (14)
5.2Why you should u prototypes (15)
5.3Autoboxing (15)
6A Really Awesome Equality15
1
7If You Have20Minutes (16)
7.1Forwarding constructors (17)
slowly的比较级7.2Iterators for cool people (17)
7.3Java class and interfaces (18)
7.4Recursive metaclass (19)
7.5Tail calls and delimited continuations (20)
7.6Syntactic macros and operator overloading (22)
8Further reading24
2
chinecommunist1Introduction
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 howfirst-class functions work.
2Types
Javascript has nine types.They are:
1.Null–null.Chucks a wobbly if you ask it for any ull.foo
fails.Never boxed.2
2.Undefined–undefined.What you get if you ask an object for something
it doesn’t istent.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
difference).Sometimes boxed.Instance of String when boxed.
4.Numbers–e.g.5,3e+10(all numbers behave asfloats–significant for
division,but can be truncated by x>>>0).Sometimes boxed.Instance
of Number when boxed.
5.Booleans–true and fal.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
ca 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 ElementById, which returns null if it can’tfind an element).Making sparing u 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 ramifications as well–
e ction4.6.
3
3Functions
Functions arefirst-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 veral 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 prent;
otherwi they’re undefined.For example:
祛斑美白方法(function(x,y){return x+y})(’foo’)//=>’fooundefined’The arguments to your function can be accesd in afirst-class way,too:
var f=function(){return arguments[0]+arguments[1]};
var g=function(){return arguments.length};
f(’foo’)//=>’fooundefined’
g(null,fal,undefined)//=>3
The arguments keyword is not an array!It just looks like one.In particular,
doing any of the will cau problems:gma
[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 u 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 lf-reference:
var f=function(){return f};
f()===f//=>true
3First-class in the n that you can pass them around as values at runtime.You can’t reliably
introspect them,however,becau while you can obtain their source code via toString you won’t
be able to access the values they clo over.
4Note that block scoping isn’t ud–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 conquence of lazy scoping is
that you can create functions that refer to variables that might never
exist.This makes Javascript very difficult 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=String().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 tofigure 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 browr.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 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 foo(5),then inside that
function’s body the word this refers to the object,in this ca x.
青岛法语培训
3.If the function starts offas 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 t it to(unless you try to t it to null or undefined,in which
ca it will be the global object again):
var f=function(){return this};
f.call(4)//=>4
f.call(0)//=>0
f.call(fal)//=>fal
f.call(null)//=>[object global]
6God forbid.
5
>成人学士学位英语

本文发布于:2023-06-06 18:34:08,感谢您对本站的认可!

本文链接:https://www.wtabcd.cn/fanwen/fan/90/136149.html

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。

标签:美白   阅读   青岛   成人   祛斑   答案
相关文章
留言与评论(共有 0 条评论)
   
验证码:
Copyright ©2019-2022 Comsenz Inc.Powered by © 专利检索| 网站地图