From 5101bb792fdd93a5eed32f6e447efcf98db2be5f Mon Sep 17 00:00:00 2001
From: shiffman Notice how the very first line of code creates a variable called Object Destructuring
-Object destructuring in JavaScript is a technique to extract properties from an object and assign them to variables. In the case of matter.js, the Object Destructuring
+ Object destructuring in JavaScript is a technique to extract properties from an object and assign them to variables. In the case of matter.js, the Engine
and sets it equal to Matter.Engine
. Here, I am deciding to point the single keyword Engine
to the Engine
class namespaced inside matter.js. I am making this decision because I know that I will not be using the word Engine
for any other variables (nor does it conflict with something in p5.js) and want to be able to keep my code less verbose. I’ll be doing this with Vector
, Bodies
, Composite
, and more as I continue to build the examples. (But while the linked source code will always include all the alias’s, I won’t always include them in the book text itself.)Matter
object contains the Engine
property. An alias can be set with let Engine = Matter.Engine
, however, by with destructuring, the Engine
can be accessed more concisely: let { Engine } = Matter
. In the examples in this chapter, I will create multiple aliases with this methodology.
+Matter
object contains the Engine
property. An alias can be set with let Engine = Matter.Engine
, however, by with destructuring, the Engine
can be accessed more concisely: let { Engine } = Matter
. In the examples in this chapter, I will create multiple aliases with this methodology.// Using Object Destructuring to extract aliases for Engine and Vector
let { Engine, Vector } = Matter;
let a = createVector(1, -1);
+
+ let a = createVector(1, -1);
let b = createVector(3, 4);
-a.add(b);
- let a = new Vec2D(1, -1);
+a.add(b);
+
+
+ let a = new Vec2D(1, -1);
let b = new Vec2D(3, 4);
-a.addSelf(b);
+a.addSelf(b);
+
let a = createVector(1, -1);
+
+ let a = createVector(1, -1);
let b = createVector(3, 4);
-let c = p5.Vector.add(a, b);
- let a = new Vec2D(1, -1);
+let c = p5.Vector.add(a, b);
+
+
+ let a = new Vec2D(1, -1);
let b = new Vec2D(3, 4);
-let c = a.add(b);
+let c = a.add(b);
+
let a = createVector(1, -1);
+
+ let a = createVector(1, -1);
let m = a.mag();
-a.normalize();
- let a = new Vec2D(1, -1);
+a.normalize();
+
+
+ let a = new Vec2D(1, -1);
let m = a.magnitude();
-a.normalize();
+a.normalize();
+
u|+eLD|A238g3M?*1`jMOgu1`gG
z;AEq7n(dYK`Iq?%PI>C&M_FH9A#73CbU%5Eu2a<2?pE}Ry$#h^B(IpR*c806` K9>aHaQk$aij!`g~r6nOU>;HkzP)?XSL-=aIJ7
zpE5EsQVcVDUT^F)%Q+T)haOut;_Ho@?WN;AVnk-LgFqX$c)7;a8OBd5nln2eum@p!
zV0ATy4y=`*GSSLgoBmM*>IN45GfS^_fbPZU9FW>ZwiF~SO^k#Frwo@^5(-{NS=PR$
ze)rW+c&tDA%p&Jy7XPbnVWkl{AAls|sx7cK5g#tjaxoC??Ck?_;tE3b`w(}Kv7r>*
zjcBXgYU34XiSTkK3}?9xzry$OF#p%^Wza#CE;Ctov-`
zSRW`3K7k2Q
Y
z(3kFO9omW$R=}Uzu>65?@$2<2*P2o=cbS{T63Z1z0D!mS>IWOWKHd2F_Dd3WGu{-b
z{_0CPkzKuC-|;=;71-LmKYRA^>VeH3RSiALt)T8R6x$}snl8nkmwKe)YIQ(iLRn$m
z74K`eO-f#NED;1e@CilFYUH-Dxp=a)m_>MQDv6iYW;eQF^
zNI9*e+gPKANpoxI8e$yRS8G^s
$?CY{{`vHXaN4=0|=C2VhRTN$!Od*~b=Okk%ev{b~e-JeMMgwBva1AP)w&3<4(
zK*KNS;Z|iBj%Za#gx1+D4HU;E%3##*4Pld_Id<4tsqCLUAKy3%+jZ
LVW