This document provides an introduction to JavaScript basics including data types, variables, operators, flow control, objects, arrays, functions, regular expressions and classes/modules. It begins with an overview of JavaScript and its relationship to ECMAScript. It then covers JavaScript syntax, variables, data types like numbers, strings, Booleans, null/undefined, arrays and objects. Methods for manipulating strings and numbers are demonstrated. The document concludes with examples and exercises for practicing JavaScript fundamentals.
Those slides, inspired by the works and ideas of many, are a state of what PHP gets in upgrades as of Q4 2018.
Market share and evolution.
PHP 7.3
PHP 7.4
PHP 8.0
I have create slides which lists examples of oops programming concepts including looping,enums,structures,linq,threading,delegates,generics,inheritenc..so on.
Everybody is talking about ES6, but is anyone out there using it in production? Mango recently started adopting ES6 features that make our front-end code easier to write and maintain.
In this talk, I will explain why we decided to use ES6, how we started to use it in production (using npm, browserify and babel) and I'll explain what problems we found along the way and how we solved them.
Links:
https://getmango.com
https://getmango.com/blog/writing-es6-modules-with-6to5/
http://kangax.github.io/compat-table/
https://github.com/zloirock/core-js
https://github.com/mango/emitter
http://wiki.ecmascript.org/doku.php?id=harmony:specification_drafts
http://benmccormick.org/2015/04/07/es6-classes-and-backbone-js/
Those slides, inspired by the works and ideas of many, are a state of what PHP gets in upgrades as of Q4 2018.
Market share and evolution.
PHP 7.3
PHP 7.4
PHP 8.0
I have create slides which lists examples of oops programming concepts including looping,enums,structures,linq,threading,delegates,generics,inheritenc..so on.
Everybody is talking about ES6, but is anyone out there using it in production? Mango recently started adopting ES6 features that make our front-end code easier to write and maintain.
In this talk, I will explain why we decided to use ES6, how we started to use it in production (using npm, browserify and babel) and I'll explain what problems we found along the way and how we solved them.
Links:
https://getmango.com
https://getmango.com/blog/writing-es6-modules-with-6to5/
http://kangax.github.io/compat-table/
https://github.com/zloirock/core-js
https://github.com/mango/emitter
http://wiki.ecmascript.org/doku.php?id=harmony:specification_drafts
http://benmccormick.org/2015/04/07/es6-classes-and-backbone-js/
Не так давно Гор Нишанов представил свой доклад: C++ Coroutines a negative overhead abstraction. В этом докладе Гор упомянул, что предложенный дизайн корутин позволяет их использовать практически в любых окружениях, в том числе и с "бедным" C++ рантаймом.
Я решил попробовать запустить корутины в следующих окружениях: обычное приложение, драйвер ОС Windows, EFI приложение. Только в одном из этих окружений есть полноценный C++ рантайм и поддержка исключений, в остальных ничего этого нет. Более того, EFI приложение вообще выполняется до старта ОС.
Я хочу рассказать о том, как мне удалось запустить корутины в этих окружениях, поговорим о том, какие проблемы существуют в асинхронном системном программировании и как их можно обойти.
Workshop JavaScript Testing. Frameworks. Client vs Server Testing. Jasmine. Chai. Nock. Sinon. Spec Runners: Karma. TDD. Code coverage. Building a testable JS app.
Presentado por ing: Raúl Delgado y Mario García
FrontDays #3. Иван Федяев, Эволюция JavaScript. Обзор нововведений ECMAScript 6FrontDays
Общий обзор новых возможностей ES6, сравнение с более ранними стандартами. Почему стоит использовать ES6 уже сейчас? Поговорим о таких нововведениях как: поддержка классов, шаблоны строк, модули, стрелочные функции, генераторы и о многом другом.
An introduction on testing Node.js code.
Covers frontend (UI) testing, backend unit tests and code coverage.
Also mentions how to use a Makefile to run frontend and backend tests at the same time.
A discussion on the upcoming new features in ES6 and how they change the way we build applications with JavaScript.
Most Notable Upcoming Changes in EcmaScript 6
Classes: Classes are now first-class citizens in ES6. The language offers support for classes ("class" keyword), constructors ("constructor" keyword) and the "extend" keyword for inheritance.
Modules: Modules provide a much needed way to segment and organize JavaScript applications in logical chunks of code. Many frameworks already provide ways to modularize large apps, but standardizing a common module structure will bring a level of interoperability between various JavaScript libraries.
Block Scoping: Scoping in JavaScript can be a confusing topic for programmers coming from more traditional Object-Oriented languages such as Java or C#. There are basically two scopes in JavaScript: global and function. Until now. With the help of the "let" keyword, ES6 enables the definition of block scopes for variables and functions.
Default Parameters: A liked feature of many other languages such as Ruby, the default parameters save endless checking of they've been passed and if they are "undefined".
Enhanced Object Literals: There is now a handy expression for property: property assignments and methods can be defined without the "function" keyword.
Не так давно Гор Нишанов представил свой доклад: C++ Coroutines a negative overhead abstraction. В этом докладе Гор упомянул, что предложенный дизайн корутин позволяет их использовать практически в любых окружениях, в том числе и с "бедным" C++ рантаймом.
Я решил попробовать запустить корутины в следующих окружениях: обычное приложение, драйвер ОС Windows, EFI приложение. Только в одном из этих окружений есть полноценный C++ рантайм и поддержка исключений, в остальных ничего этого нет. Более того, EFI приложение вообще выполняется до старта ОС.
Я хочу рассказать о том, как мне удалось запустить корутины в этих окружениях, поговорим о том, какие проблемы существуют в асинхронном системном программировании и как их можно обойти.
Workshop JavaScript Testing. Frameworks. Client vs Server Testing. Jasmine. Chai. Nock. Sinon. Spec Runners: Karma. TDD. Code coverage. Building a testable JS app.
Presentado por ing: Raúl Delgado y Mario García
FrontDays #3. Иван Федяев, Эволюция JavaScript. Обзор нововведений ECMAScript 6FrontDays
Общий обзор новых возможностей ES6, сравнение с более ранними стандартами. Почему стоит использовать ES6 уже сейчас? Поговорим о таких нововведениях как: поддержка классов, шаблоны строк, модули, стрелочные функции, генераторы и о многом другом.
An introduction on testing Node.js code.
Covers frontend (UI) testing, backend unit tests and code coverage.
Also mentions how to use a Makefile to run frontend and backend tests at the same time.
A discussion on the upcoming new features in ES6 and how they change the way we build applications with JavaScript.
Most Notable Upcoming Changes in EcmaScript 6
Classes: Classes are now first-class citizens in ES6. The language offers support for classes ("class" keyword), constructors ("constructor" keyword) and the "extend" keyword for inheritance.
Modules: Modules provide a much needed way to segment and organize JavaScript applications in logical chunks of code. Many frameworks already provide ways to modularize large apps, but standardizing a common module structure will bring a level of interoperability between various JavaScript libraries.
Block Scoping: Scoping in JavaScript can be a confusing topic for programmers coming from more traditional Object-Oriented languages such as Java or C#. There are basically two scopes in JavaScript: global and function. Until now. With the help of the "let" keyword, ES6 enables the definition of block scopes for variables and functions.
Default Parameters: A liked feature of many other languages such as Ruby, the default parameters save endless checking of they've been passed and if they are "undefined".
Enhanced Object Literals: There is now a handy expression for property: property assignments and methods can be defined without the "function" keyword.
Game Design and Development Workshop Day 1Troy Miles
This course teaches you how to build awesome video games using Cocos2Dx. Cocos2Dx is a feature packed, free game development engine. It is cross platform, high performance, and supports three languages: C++, Lua and JavaScript. With it, you can write games for iOS, Android, Windows, Mac, and the Web. It is nothing short of amazing and this course teaches you how to use it. We will program Cocos2Dx in JavaScript, but this is not a programming class. If you don't know how to write code in JavaScript or some other curly brace language, this is not the course for you. For those whose JavaScript is rusty, we will do quick language overview. Don't let the JavaScript part fool you. Cocos2Dx is built from highly optimized C++ and OpenGL, JavaScript is used for game logic, not graphics. Our games will run at a super fast, 60 frames a second.
Rust
Why do you care about Rust? Who has the time to learn all these new languages? It seems like a new one is popping up every other week and this trend is growing at an exponential rate. Good news, a fair number of them are crafted really well and efficiently solve specific problems. Bad news, how do you keep up with all of this, let alone decide which languages to include in your companies technology portfolio.
Despite the challenges of all these new languages, a majority of developers are intrigued about the idea of becoming a polyglot, but don't know where to begin or don't have the time. In my polyglot travels, there is one language of late that is the sure-fire answer to the above questions, Rust.
In this talk I’ll explore the value behind becoming more polyglotic as a developer, how to pick languages to learn, and then dive deep in the the language of Rust. Which in my opinion, is hands down the best up and coming languages to learn.
About the Presenter
Anthony Broad-Crawford has been a developer since the year 2000 with a short side stint as a semi-professional poker player. Since his transition to software development Anthony has...
1. Built 8 patent receiving technologies
2. Founded two global companies
3. Been a CTO (3x), CPO (1x), and CEO (1x)
and is currently the CTO at Fooda where he manages product, user experience, and engineering. Fooda is predominantly web and mobile technology company focused on bringing great & healthy food from the best restaurant's to people while at the office.
Through his career, in production applications Anthony has used Ruby, Java, Jave (Android), Objective-C and Swift, .NET, Erlang, Scala, Node.JS, LISP, Smalltalk, and even assembly, with his recent favorite, Rust . No, not all at the same time in the same application.
Anthony now spends his time building great teams, that leverage great technology, to build great products, but still looks to codes every chance he can get :)
JavaScript - An Introduction is a beginner's guide to JavaScript. It starts with very basic level and goes to intermediate level. You'll be introduced with every language constructs, Event handling, Form handling and AJAX which is supported by JavaScript with XMLHttpRequest object. This XHR object is discussed in enough detail so that you can understand how the underlying AJAX functionality works in jQuery. At the end it discusses advance concepts and library build on/around JavaScript.
function,built in function,defination of arguments,why we use default arguments,call by value,call by reference,defination of recursive function,variable scope, user defined function,date/time function
why function,built in function,arguments,default arguments,call by value,call by reference,recursive function,variable scope,user defined function,date/time function
Dev Dives: Train smarter, not harder – active learning and UiPath LLMs for do...UiPathCommunity
💥 Speed, accuracy, and scaling – discover the superpowers of GenAI in action with UiPath Document Understanding and Communications Mining™:
See how to accelerate model training and optimize model performance with active learning
Learn about the latest enhancements to out-of-the-box document processing – with little to no training required
Get an exclusive demo of the new family of UiPath LLMs – GenAI models specialized for processing different types of documents and messages
This is a hands-on session specifically designed for automation developers and AI enthusiasts seeking to enhance their knowledge in leveraging the latest intelligent document processing capabilities offered by UiPath.
Speakers:
👨🏫 Andras Palfi, Senior Product Manager, UiPath
👩🏫 Lenka Dulovicova, Product Program Manager, UiPath
Securing your Kubernetes cluster_ a step-by-step guide to success !KatiaHIMEUR1
Today, after several years of existence, an extremely active community and an ultra-dynamic ecosystem, Kubernetes has established itself as the de facto standard in container orchestration. Thanks to a wide range of managed services, it has never been so easy to set up a ready-to-use Kubernetes cluster.
However, this ease of use means that the subject of security in Kubernetes is often left for later, or even neglected. This exposes companies to significant risks.
In this talk, I'll show you step-by-step how to secure your Kubernetes cluster for greater peace of mind and reliability.
The Art of the Pitch: WordPress Relationships and SalesLaura Byrne
Clients don’t know what they don’t know. What web solutions are right for them? How does WordPress come into the picture? How do you make sure you understand scope and timeline? What do you do if sometime changes?
All these questions and more will be explored as we talk about matching clients’ needs with what your agency offers without pulling teeth or pulling your hair out. Practical tips, and strategies for successful relationship building that leads to closing the deal.
LF Energy Webinar: Electrical Grid Modelling and Simulation Through PowSyBl -...DanBrown980551
Do you want to learn how to model and simulate an electrical network from scratch in under an hour?
Then welcome to this PowSyBl workshop, hosted by Rte, the French Transmission System Operator (TSO)!
During the webinar, you will discover the PowSyBl ecosystem as well as handle and study an electrical network through an interactive Python notebook.
PowSyBl is an open source project hosted by LF Energy, which offers a comprehensive set of features for electrical grid modelling and simulation. Among other advanced features, PowSyBl provides:
- A fully editable and extendable library for grid component modelling;
- Visualization tools to display your network;
- Grid simulation tools, such as power flows, security analyses (with or without remedial actions) and sensitivity analyses;
The framework is mostly written in Java, with a Python binding so that Python developers can access PowSyBl functionalities as well.
What you will learn during the webinar:
- For beginners: discover PowSyBl's functionalities through a quick general presentation and the notebook, without needing any expert coding skills;
- For advanced developers: master the skills to efficiently apply PowSyBl functionalities to your real-world scenarios.
Builder.ai Founder Sachin Dev Duggal's Strategic Approach to Create an Innova...Ramesh Iyer
In today's fast-changing business world, Companies that adapt and embrace new ideas often need help to keep up with the competition. However, fostering a culture of innovation takes much work. It takes vision, leadership and willingness to take risks in the right proportion. Sachin Dev Duggal, co-founder of Builder.ai, has perfected the art of this balance, creating a company culture where creativity and growth are nurtured at each stage.
DevOps and Testing slides at DASA ConnectKari Kakkonen
My and Rik Marselis slides at 30.5.2024 DASA Connect conference. We discuss about what is testing, then what is agile testing and finally what is Testing in DevOps. Finally we had lovely workshop with the participants trying to find out different ways to think about quality and testing in different parts of the DevOps infinity loop.
Epistemic Interaction - tuning interfaces to provide information for AI supportAlan Dix
Paper presented at SYNERGY workshop at AVI 2024, Genoa, Italy. 3rd June 2024
https://alandix.com/academic/papers/synergy2024-epistemic/
As machine learning integrates deeper into human-computer interactions, the concept of epistemic interaction emerges, aiming to refine these interactions to enhance system adaptability. This approach encourages minor, intentional adjustments in user behaviour to enrich the data available for system learning. This paper introduces epistemic interaction within the context of human-system communication, illustrating how deliberate interaction design can improve system understanding and adaptation. Through concrete examples, we demonstrate the potential of epistemic interaction to significantly advance human-computer interaction by leveraging intuitive human communication strategies to inform system design and functionality, offering a novel pathway for enriching user-system engagements.
Accelerate your Kubernetes clusters with Varnish CachingThijs Feryn
A presentation about the usage and availability of Varnish on Kubernetes. This talk explores the capabilities of Varnish caching and shows how to use the Varnish Helm chart to deploy it to Kubernetes.
This presentation was delivered at K8SUG Singapore. See https://feryn.eu/presentations/accelerate-your-kubernetes-clusters-with-varnish-caching-k8sug-singapore-28-2024 for more details.
2. Javascript
"JavaScript" is an ECMAScript variant
managed by Mozilla. All non-Mozilla
implementations using JavaScript are
actually implementing ECMAScript, rather
than Javascript. "JavaScript" support
generally is meant to describe support
for ECMA-262, edition 3, though some—
notably Chrome, but also Opera 12 and
Internet Explorer 10—target ECMA-262,
edition 5.
11. Number 操作
Math 提供許多常⽤用的運算函式
Math.pow(2,53)
//
=>
9007199254740992:
2
to
the
power
53
Math.round(.6)
//
=>
1.0:
round
to
the
nearest
integer
Math.ceil(.6)
//
=>
1.0:
round
up
to
an
integer
Math.floor(.6)
//
=>
0.0:
round
down
to
an
integer
Math.abs(-‐5)
//
=>
5:
absolute
value
Math.max(x,y,z)
//
Return
the
largest
argument
Math.min(x,y,z)
//
Return
the
smallest
argument
Math.random()
//
Pseudo-‐random
number
x
where
0
<=
x
<
1.0
Math.PI
//
π:
circumference
of
a
circle
/
diameter
Math.E
//
e:
The
base
of
the
natural
logarithm
Math.sqrt(3)
//
The
square
root
of
3
Math.pow(3,
1/3)
//
The
cube
root
of
3
Math.sin(0)
//
Trigonometry:
also
Math.cos,
Math.atan,
etc.
Math.log(10)
//
Natural
logarithm
of
10
Math.log(100)/Math.LN10
//
Base
10
logarithm
of
100
Math.log(512)/Math.LN2
//
Base
2
logarithm
of
512
Math.exp(3)
//
Math.E
cubed
15. String 範例
var
s
=
"hello,
world"
//
Start
with
some
text.
s.charAt(0)
//
=>
"h":
the
first
character.
s.charAt(s.length-‐1)
//
=>
"d":
the
last
character.
s.substring(1,4)
//
=>
"ell":
the
2nd,
3rd
and
4th
characters.
s.slice(1,4)
//
=>
"ell":
same
thing
s.slice(-‐3)
//
=>
"rld":
last
3
characters
s.indexOf("l")
//
=>
2:
position
of
first
letter
l.
s.lastIndexOf("l")
//
=>
10:
position
of
last
letter
l.
s.indexOf("l",
3)
//
=>
3:
position
of
first
"l"
at
or
after
3
s.split(",
")
//
=>
["hello",
"world"]
split
into
substrings
s.replace("h",
"H")
//
=>
"Hello,
world":
replaces
all
instances
s.toUpperCase()
//
=>
"HELLO,
WORLD"
23. Variable Scope
Javascript的變數範圍是以function來界定
var
scope
=
"global";
//
Declare
a
global
variable
function
checkscope()
{
var
scope
=
"local";
//
Declare
a
local
variable
with
the
same
name
return
scope;
//
Return
the
local
value,
not
the
global
one
}
console.log(checkscope());
//
=>
"local"
28. Expression 運算式
//property
access
var
o
=
{x:1,
y:{z:3}};
var
a
=
[o,4,[5,6]];
o.y;
a[0].x;
//invocation
expression
f(0);
Math.max(x,y,z);
//object
creation
new
Date();
new
Point(2,3);
39. Statement 宣告
var
function
var
i;
var
greeting
=
"hello"
+
name;
var
x
=
2.34,
y
=
Math.cos(0.75),
r,
theta;
var
x
=
2,
y
=
x*x;
var
f
=
function(x)
{
return
x*x
},
y
=
f(x);
for(var
i
=
0;
i
<
10;
i++)
console.log(i);
for(var
i
=
0,
j=10;
i
<
10;
i++,j-‐-‐)
console.log(i*j);
for(var
i
in
o)
console.log(i);
function
hypotenuse(x,
y)
{
return
Math.sqrt(x*x
+
y*y);
//
return
is
documented
in
the
next
section
}
function
factorial(n)
{
//
A
recursive
function
if
(n
<=
1)
return
1;
return
n
*
factorial(n
-‐
1);
}
47. Statement 迴圈
for
for/in: ⽤用在列舉物件屬性時
for(var
count
=
0;
count
<
10;
count++)
console.log(count);
for(var
i
=
0,
j
=
10
;
i
<
10
;
i++,
j-‐-‐)
sum
+=
i
*
j;
for(var
p
in
o)
//
Assign
property
names
of
o
to
variable
p
ßconsole.log(o[p]);
//
Print
the
value
of
each
property
var
o
=
{x:1,
y:2,
z:3};
var
a
=
[],
i
=
0;
for(a[i++]
in
o)
/*
empty
*/;
49. Statement 跳脫
break
for(var
i
=
0;
i
<
a.length;
i++)
{
if
(a[i]
==
target)
break;
}
switch(n)
{
case
1:
//
Start
here
if
n
==
1
//
Execute
code
block
#1.
break;
case
2:
//
Execute
code
block
#2.
break;
case
3:
//
Execute
code
block
#3.
break;
default:
//
Execute
code
block
#4.
break;
}
50. Statement 跳脫
continue
return
for(i
=
0;
i
<
data.length;
i++)
{
if
(!data[i])
continue;
//
Can't
proceed
with
undefined
data
total
+=
data[i];
}
function
display_object(o)
{
if
(!o)
return;
console.dir(o);
return;
}
51. Statement 跳脫
throw
function
factorial(x)
{
//
If
the
input
argument
is
invalid,
throw
an
exception!
if
(x
<
0)
throw
new
Error("x
must
not
be
negative");
//
Otherwise,
compute
a
value
and
return
normally
for(var
f
=
1;
x
>
1;
f
*=
x,
x-‐-‐)
/*
empty
*/
;
return
f;
}
52. Statement 跳脫
try/catch/finally
function
factorial(x)
{
var
f
=
1;
try
{
if
(x
<
0)
throw
new
Error("x
must
not
be
negative");
for(;
x
>
1;
f
*=
x,
x-‐-‐)
/*
empty
*/
;
return
f;
}
catch(e)
{
if(e
instanceof
Error)
{
console.log(e);
return
undefined;
}
}
finally
{
console.log("Exit
function");
}
}
55. Object 物件
基本的物件宣告:
var empty = {};
var point = { x:0, y:0 };
物件式宣告:
var a = new Object();(不建議使⽤用)
var b = new Date();
var c = new RegExp("js");
56. Object 物件屬性
//get
properties
from
object
var
book
=
{
author:
{firstname:
"J.R.R.",
lastname:
"Tolkien"},
"main
title":
"The
Lord
of
the
Rings"
};
var
author
=
book.author;
//
Get
the
"author"
property
of
the
book.
var
name
=
author.lastname;
//
Get
the
"surname"
property
of
the
author.
var
title
=
book["main
title"];
//
Get
the
"main
title"
property
of
the
book.
console.log(name,
title);
//set
properties
of
object
book["sub
title"]=
"The
Fellowship
of
the
Ring";
book.price
=
131;
console.log(book["main
title"],
book["sub
title"]);
57. Object 存取屬性
//get
properties
from
object
var
book
=
{
author:
{firstname:
"J.R.R.",
lastname:
"Tolkien"},
"main
title":
"The
Lord
of
the
Rings"
};
var
subtitle
=
book.subtitle;
//error
var
len
=
book.subtitle.length;
//
A
concise
and
idiomatic
alternative
to
get
subtitle
length
or
undefined
var
len
=
book
&&
book.subtitle
&&
book.subtitle.length;
console.log(subtitle,
len);
58. 移除物件屬性
全域物件無法移除
Object delete
//delete:
remove
props
from
an
object
var
book
=
{
author:
{firstname:
"J.R.R.",
lastname:
"Tolkien"},
"main
title":
"The
Lord
of
the
Rings"
};
delete
book.author;
//
The
book
object
now
has
no
author
property.
delete
book["main
title"];
//
Now
it
doesn't
have
"main
title",
either.
console.dir(book);
59. Object 測試屬性
in
hasOwnProperty
propertyIsEnumerable
var
o
=
{
x:
1
}
"x"
in
o;
//
true:
o
has
an
own
property
"x"
"y"
in
o;
//
false:
o
doesn't
have
a
property
"y"
"toString"
in
o;
//
true:
o
inherits
a
toString
property
var
o
=
{
x:
1
}
o.hasOwnProperty("x");
//
true:
o
has
an
own
property
x
o.hasOwnProperty("y");
//
false:
o
doesn't
have
a
property
y
o.hasOwnProperty("toString");
//
false:
toString
is
an
inherited
property
var
o
=
inherit({
y:
2
});
o.x
=
1;
o.propertyIsEnumerable("x");
//
true:
o
has
an
own
enumerable
property
x
o.propertyIsEnumerable("y");
//
false:
y
is
inherited,
not
own
Object.prototype.propertyIsEnumerable("toString");
//
false:
not
enumerable
60. Object 列舉屬性
列出此物件的所有屬性
//
在for
loop裡的in只會列出enumerable的屬性
//
ex:
前例的toString不會出現在loop中
for(var
i
in
o)
{
if
(foo.hasOwnProperty(i))
{
console.log(i);
}
}
//
列出物件的屬性,且⾮非函式
for(var
i
in
o)
{
if
(foo.hasOwnProperty(i)
&&
typeof
o[i]
!==
'function')
{
console.log(i);
}
}
62. Array 陣列
基本的陣列宣告:
var myArray = [];
var myArray2 = [1, {}, true];
物件式宣告:(不建議使⽤用)
var a = new Array();
var b = new Array(5);
var c = new Array(4, "1", true);
63. Iterating Array
//針對⼀一般陣列
for(var
i
=
0;
i
<
a.length;
i++)
{
if
(!(i
in
a))
continue
;
//
跳過未定義的index
//
loop
body
here
}
//針對sparse
array
for(var
index
in
sparseArray)
{
var
value
=
sparseArray[index];
//
Now
do
something
with
index
and
value
}
65. Array Methods
var
a
=
[1,
2,
3];
a.join();
a.join("
");
a.join("");
var
b
=
new
Array(10);
b.join('-‐')
var
a
=
[1,2,3];
a.reverse().join()
//
=>
"3,2,1"
and
a
is
now
[3,2,1]
66. Array Methods
//sort
with
alphabetical
order
var
a
=
new
Array("banana",
"cherry",
"apple");
a.sort();
var
s
=
a.join(",
");
//
s
==
"apple,
banana,
cherry"
var
a
=
[1,2,3];
a.reverse().join()
//
=>
"3,2,1"
and
a
is
now
[3,2,1]
var
a
=
[33,
4,
1111,
222];
a.sort();
//
Alphabetical
order:
1111,
222,
33,
4
a.sort(function(a,b)
{
//
Numerical
order:
4,
33,
222,
1111
return
a-‐b;
//
Returns
<
0,
0,
or
>
0,
depending
on
order
});
a.sort(function(a,b)
{return
b-‐a});
//
Reverse
numerical
order
67. Array Methods
var
a
=
[1,2,3];
a.concat(4,
5)
a.concat([4,5]);
a.concat([4,5],[6,7])
a.concat(4,
[5,[6,7]])
var
a
=
[1,2,3,4,5];
a.slice(0,3);
//
Returns
[1,2,3]
a.slice(3);
//
Returns
[4,5]
a.slice(1,-‐1);
//
Returns
[2,3,4]
a.slice(-‐3,-‐2);
//
Returns
[3]
//slice可以拿來複製陣列
var
a
=
[1,2,3,4,5];
var
b
=
a.slice(0);
b[4]
=
0;
console.log(a,
b);
//[1,2,3,4,5]
[1,2,3,4,0]
70. Array Methods
(ECMAScript 5)
//
forEach()
//
array
iterator
var
data
=
[1,2,3,4,5];
//
An
array
to
sum
//
Compute
the
sum
of
the
array
elements
var
sum
=
0;
//
Start
at
0
data.forEach(function(value)
{
sum
+=
value;
});
//
Add
each
value
to
sum
sum
//
=>
15
//
Now
increment
each
array
element
data.forEach(function(v,
i,
a)
{
a[i]
=
v
+
1;
});
console.log(data);
//
=>
[2,3,4,5,6]
//map
//計算並產⽣生⼀一個新的陣列
a
=
[1,
2,
3];
b
=
a.map(function(x)
{
return
x*x;
});
//
b
is
[1,
4,
9]
71. Array Methods
(ECMAScript 5)
//filter
a
=
[5,
4,
3,
2,
1];
smallvalues
=
a.filter(function(x)
{
return
x
<
3
});
//
[2,
1]
everyother
=
a.filter(function(x,i)
{
return
i%2==0
});
//
[5,
3,
1]
//every
//檢查是否每個元素都符合條件
a
=
[1,2,3,4,5];
a.every(function(x)
{
return
x
<
10;
})
//
=>
true:
all
values
<
10.
a.every(function(x)
{
return
x
%
2
===
0;
})
//
=>
false:
not
all
values
even.
//some
//檢查是否有元素符合條件
a
=
[1,2,3,4,5];
a.some(function(x)
{
return
x%2===0;
})
//
=>
true
a
has
some
even
numbers.
a.some(isNaN)
//
=>
false:
a
has
no
non-‐numbers.
72. Array Methods
(ECMAScript 5)
//array.reduce(callback,
[initialValue])
//callback
=
function(previousValue,
currentValue,
index,
array)
var
a
=
[1,2,3,4,5]
var
sum
=
a.reduce(function(x,y)
{
return
x+y
},
0);
//
Sum
of
values
var
product
=
a.reduce(function(x,y)
{
return
x*y
},
1);
//
Product
of
values
var
max
=
a.reduce(function(x,y)
{
return
(x>y)?x:y;
});
//
Largest
value
//reduceRight()
var
a
=
[2,
3,
4]
//
Compute
2^(3^4).
Exponentiation
has
right-‐to-‐left
precedence
var
big
=
a.reduceRight(function(accumulator,value)
{
return
Math.pow(value,accumulator);
});
練習:使⽤用reduce把陣列[1,2,3,4,5]變成字串
12345
73. Array Methods
(ECMAScript 5)
//
Find
all
occurrences
of
a
value
x
in
an
array
a
and
return
an
array
//
of
matching
indexes
function
findall(a,
x)
{
var
results
=
[],
//
The
array
of
indexes
we'll
return
len
=
a.length,
//
The
length
of
the
array
to
be
searched
pos
=
0;
//
The
position
to
search
from
while(pos
<
len)
{
//
While
more
elements
to
search...
pos
=
a.indexOf(x,
pos);
//
Search
if
(pos
===
-‐1)
break;
//
If
nothing
found,
we're
done.
results.push(pos);
//
Otherwise,
store
index
in
array
pos
=
pos
+
1;
//
And
start
next
search
at
next
element
}
return
results;
//
Return
array
of
indexes
}
//array.lastIndexOf(searchElement[,
fromIndex])
a
=
[0,1,2,1,0];
a.indexOf(1)
//
=>
1:
a[1]
is
1
a.lastIndexOf(1)
//
=>
3:
a[3]
is
1
a.indexOf(3)
//
=>
-‐1:
no
element
has
value
3
77. Function 呼叫
直接呼叫
var
probability
=
factorial(5)/factorial(13);
var
strict
=
(function()
{
return
!this;
}());
var
calculator
=
{
//
An
object
literal
operand1:
1,
operand2:
1,
add:
function()
{
//
Note
the
use
of
the
this
keyword
to
refer
to
this
object.
this.result
=
this.operand1
+
this.operand2;
}
};
calculator.add();
//
A
method
invocation
to
compute
1+1.
calculator.result
//
=>
2
78. Function 呼叫
建構式
var
probability
=
factorial(5)/factorial(13);
var
strict
=
(function()
{
return
!this;
}());
function
Person(name,
age)
{
this.name
=
name;
this.age
=
age;
};
var
bob
=
new
Person("Bob
Dylan",
72);
bob.name;
//"Bob
Dylan
bob.age;
//72
79. Function 呼叫
call, apply
function
Person(name,
age)
{
this.name
=
name;
this.age
=
age;
this.greet
=
function(greeter)
{
return
"Hi,
"
+
greeter
+
".
I'm
"
+
this.name;
}
};
var
bob
=
new
Person("Bob
Dylan",
72);
var
hans
=
new
Person("Hans
Zimmer",
55);
bob.greet.call(hans,
"Fin");
hans.greet.apply(bob,
["Fin"]);
80. Function 參數
參數可以是任意數,⽤用arguments來取
function
max(/*
...
*/)
{
var
max
=
Number.NEGATIVE_INFINITY;
//
Loop
through
the
arguments,
looking
for,
and
remembering,
the
biggest.
for(var
i
=
0;
i
<
arguments.length;
i++)
if
(arguments[i]
>
max)
max
=
arguments[i];
//
Return
the
biggest
return
max;
}
var
largest
=
max(1,
10,
100,
2,
3,
1000,
4,
5,
10000,
6);
//
=>
10000
81. Function 參數
常⽤用pattern
function
newObj(arg,
option)
{
var
defaultOption
=
{
option1:
value1,
option2:
value2,
option3:
value3
}
//combine
defaultOption
and
option
//use
arg
and
option
to
run
newObj
}
82. Function 調⽤用
宣告為匿名函式並執⾏行,不占⽤用namespace
bind
(function(x,y)
{
//
mymodule
function
rewritten
as
an
unnamed
expression
//
Module
code
goes
here.
}(1,2));
//
end
the
function
literal
and
invoke
it
now.
function
f(y)
{
return
this.x
+
y;
};
var
o
=
{
x
:
1
};
var
g
=
f.bind(o);
g(2);
85. Class 類別
A Simple Javascript class:
function
Range(from,
to)
{
//
These
are
noninherited
properties
that
are
unique
to
this
object.
this.from
=
from;
this.to
=
to;
}
Range.prototype
=
{
constructor:
Range,
includes:
function(x)
{
return
this.from
<=
x
&&
x
<=
this.to;
},
foreach:
function(f)
{
for(var
x
=
Math.ceil(this.from);
x
<=
this.to;
x++)
f(x);
},
toString:
function()
{
return
"("
+
this.from
+
"..."
+
this.to
+
")";
}
};
//
Here
are
example
uses
of
a
range
object
var
r
=
new
Range(1,3);
//
Create
a
range
object
r.includes(2);
//
=>
true:
2
is
in
the
range
r.foreach(console.log);
//
Prints
1
2
3
console.log(r.toString());
//
Prints
(1...3)
89. Class 類別
Complex.prototype.toString
=
function()
{
return
"{"
+
this.r
+
","
+
this.i
+
"}";
};
//
Test
whether
this
Complex
object
has
the
same
value
as
another.
Complex.prototype.equals
=
function(that)
{
return
that
!=
null
&&
that.constructor
===
Complex
&&
this.r
===
that.r
&&
this.i
===
that.i;
};
Complex.ZERO
=
new
Complex(0,0);
Complex.ONE
=
new
Complex(1,0);
Complex.I
=
new
Complex(0,1);
Complex.parse
=
function(s)
{
try
{
//
Assume
that
the
parsing
will
succeed
var
m
=
Complex._format.exec(s);
//
Regular
expression
magic
return
new
Complex(parseFloat(m[1]),
parseFloat(m[2]));
}
catch
(x)
{
//
And
throw
an
exception
if
it
fails
throw
new
TypeError("Can't
parse
'"
+
s
+
"'
as
a
complex
number.");
}
};
Complex._format
=
/^{([^,]+),([^}]+)}$/;
如果要把toString改成{1, 5i}這種格式要如何改
90. Class 類別
寫⼀一個Cartesian類別
obj1 = new Cartesian(x1,y1), obj2 = new
Cartesian(x2,y2)
obj1.distance() 計算與原點的距離
obj1.distance(obj2) 計算兩點距離
obj1.x, obj2.y 取得x,y
obj1.toString() = {x, y}
obj1.furthur(obj2) = true 如果距離原點較遠
Cartesian.ORIGIN 為原點
97. RegExp in String
search
replace
match
split
console.log("JavaScript".search(/script/i));
console.log("jAvaSCRipt
claZZ".replace(/javascript/gi,
"JavaScript"));
console.log("hello
_there_
and
_here_".replace(/_(.*?)_/g,
"<div>$1</div>"));
var
url
=
/(w+)://([w.]+)/(S*)/;
var
text
=
"https://www.facebook.com/thingsaboutwebdev";
var
result
=
text.match(url);
console.log(result);
var
words
=
'How
are
you
doing,
john?'.split(/[s,?.]+/);
console.log(words);
98. RegExp 物件
exec: 執⾏行結果像是String.match,但如果有
g flag,則可以連續執⾏行取得多筆結果
test: boolean test
var
pattern
=
/Java/g;
var
text
=
"JavaScript
is
more
fun
than
Java!";
var
result;
while((result
=
pattern.exec(text))
!=
null)
{
console.log("Matched
'"
+
result[0]
+
"'"
+
"
at
position
"
+
result.index
+
";
next
search
begins
at
"
+
pattern.lastIndex);
}
var
pattern
=
/java/i;
pattern.test("JavaScript");
//
Returns
true