Bliss includes several static methods (on the Bliss or $ object). For example, to copy all properties of an object onto another object, you would call $.extend():
-
var yolo = $.extend({foo: 1}, {bar: 2}); // or Bliss.extend(…)
+
let yolo = $.extend({foo: 1}, {bar: 2}); // or Bliss.extend(…)
// yolo is {foo: 1, bar: 2}
Many of Bliss’ methods take an element or an array of elements as their first argument. For example, to set both the width and padding of an element to 0, you could use the $.style() method:
@@ -206,7 +206,7 @@
$()
Select an element by selector. Mainly a shortcut for element.querySelector().
-
var element = $(selector [, context])
+
let element = $(selector [, context])
selector
@@ -219,32 +219,30 @@
$()
The matched element or null if none found.
-
// return the first element with a class of .foo
+
// Return the first element with a class of .foo
// that is inside the first element with a class of .bar
-var ret = $(".foo", $(".bar"));
+let ret = $(".foo", $(".bar"));
// Return the first element with a class of .foo
// that is inside any element with a class of .bar
-var ret = $(".bar .foo");
+let ret = $(".bar .foo");
// Get the first element with a class of .foo
-// and set its title attribute to "yolo"
+// and set its title attribute to "yolo".
// If there is no such element, this will throw an exception!
$(".foo").setAttribute("title", "yolo");
If the element is not found, it will return null. This could cause an exception if methods are called on the result.
You might previously check if the element exists by caching the node in a variable
- into an if statement:
+ into an if statement or use the optional chaining operator ?.
+ (for supporting browsers):
-
// Check if the .foo element exists
-// and set an attribute by using a variable "foo"
-// as a reference of the element
-var foo = $(".foo");
-if (foo) {
- foo.setAttribute("title", "yolo");
-}
+
// Get the first element with a class of .foo
+// and, if it exists, set its title attribute to "yolo".
+// If there is no such element, this will return "undefined"
+$(".foo")?.setAttribute("title", "yolo");
Get all elements that match a given selector as an array. Similar to element.querySelectorAll() but returns an Array instead of a NodeList, so it has all of the convenience methods we’ve come to love on arrays.
-
var array = $$(selector [, context])
+
let array = $$(selector [, context])
selector
@@ -267,7 +265,7 @@
$$()
The matched elements as an array.
-
var array = $$(collection)
+
let array = $$(collection)
collection
@@ -279,19 +277,15 @@
$$()
// Add an id to all <h1> headings that don’t already have one
-$$("h1:not([id])").forEach(function(h1){
- h1.id = h1.textContent.replace(/\W/g, "");
-});
// Get an array with all ids on the page
-var ids = $$("[id]").map(function(element){
- return element.id;
-});
+let ids = $$("[id]").map(element => element.id);
// Get all of an element’s attributes starting with data-bliss-
-$$(element.attributes).filter(function(attribute){
- return attribute.name.indexOf("data-bliss-") === 0;
-}).map(function(attribute){
- return attribute.name;
-});
Set multiple CSS properties with a transition and execute code after the transition is finished.
-
var promise = $.transition(subject, properties [, duration])
-
var promise = subject._.transition(properties [, duration])
+
let promise = $.transition(subject, properties [, duration])
+
let promise = subject._.transition(properties [, duration])
subject
@@ -985,7 +950,7 @@
ready
Execute code after the DOM is ready.
-
var promise = $.ready([context], [callback])
+
let promise = $.ready([context], [callback])
context
@@ -1011,8 +976,8 @@
when
Defer code until an event fires.
-
var promise = $.when(subject, type [, test])
-
var promise = subject._.when(type [, test])
+
let promise = $.when(subject, type [, test])
+
let promise = subject._.when(type [, test])
subject
@@ -1046,8 +1011,8 @@
all
Run a method on all elements of an array, get the results as an array.
-
var ret = $.all(array, method [, args...])
-
var ret = array._.all(method [, args...])
+
let ret = $.all(array, method [, args...])
+
let ret = array._.all(method [, args...])
array
@@ -1078,7 +1043,7 @@
Class
Helper for defining OOP-like “classes”, for those who have been irreparably damaged by Java-like languages.
-
var myClass = $.Class(options);
+
let myClass = $.Class(options);
options
@@ -1118,7 +1083,7 @@
each
Loop over properties of an object and map them onto another object.
-
var ret = $.each(obj, callback [, ret]);
+
let ret = $.each(obj, callback [, ret]);
obj
@@ -1154,11 +1119,11 @@
extend
If array or string, a whitelist of property names. If function, it is called on each property and only the properties it returns a truthy value on will be copied. If it’s a regular expression, only matching property names will be copied.
-
var o1 = {foo: 1, bar:2}
+
let o1 = {foo: 1, bar:2}
o2 = $.extend(o1, {foo: 3, baz: 4});
// o2 is {foo: 3, bar: 2, baz: 4}
// Get typography-related computed style on <body>
-var type = $.extend({},
+let type = $.extend({},
getComputedStyle(document.body),
/^font|^lineHeight$/);
@@ -1235,7 +1200,7 @@
type
Determine the internal JavaScript [[Class]] of an object.
-
var type = $.type(object)
+
let type = $.type(object)
object
@@ -1256,7 +1221,7 @@
type
value
Get the value of a nested property reference if it exists, and undefined without any errors if not.
-
var val = $.value([obj, ] property1, [property2 [,...[, propertyN]]])
+
let val = $.value([obj, ] property1, [property2 [,...[, propertyN]]])
obj
@@ -1280,7 +1245,7 @@
fetch
Helper for AJAX calls, inspired by the new Fetch API.
-
var promise = $.fetch(url, options)
+
let promise = $.fetch(url, options)
url
@@ -1331,7 +1296,7 @@
load
Include a CSS or JS resource dynamically, only once, and run code after it has loaded.
-
var promise = $.load(url [, base])
+
let promise = $.load(url [, base])
url
@@ -1352,7 +1317,7 @@
include
Include a script file conditionally and run code after it has loaded.
-
var promise = $.include([condition,] url)
+
let promise = $.include([condition,] url)
condition
@@ -1366,16 +1331,16 @@
include
// Load ES5-shim if needed
-var url = "https://cdnjs.cloudflare.com/ajax/libs/es5-shim/4.3.1/es5-sham.min.js";
+let url = "https://cdnjs.cloudflare.com/ajax/libs/es5-shim/4.3.1/es5-sham.min.js";
$.include(Array.prototype.forEach, url).then(function(){
$$("div").forEach(function(){
// some code here
});
});
// Load dropbox.js from a CDN if it’s not already loaded and run some code that uses it
-var url = "https://cdnjs.cloudflare.com/ajax/libs/dropbox.js/0.10.2/dropbox.min.js";
+let url = "https://cdnjs.cloudflare.com/ajax/libs/dropbox.js/0.10.2/dropbox.min.js";
$.include(self.Dropbox, url).then(function(){
- var client = new Dropbox.Client({ key: key });
+ let client = new Dropbox.Client({ key: key });
// ...
});
@@ -1439,7 +1404,7 @@
overload
-
var method = Bliss.overload(function (key, val) {
+