@@ -1638,7 +1638,10 @@ myNums.setData();
16381638console.log(myNums.toString());
16391639` ` `
16401640
1641- 1. 冒泡排序
1641+ ## 08 - 02
1642+ ### 算法测试- 冒泡排序
1643+
1644+ * 冒泡排序
16421645* 最慢的排序算法之一
16431646* 冒泡排序,之所以这么叫是因为使用这种排序算法排序时,数据值就会像气泡一样从数组的一端漂浮到另一端。假设正在将一组数字按照升序排列,较大的值会浮动到数组的右侧,而较小的值会浮动到数组的左侧。之所以会产生这种现象是因为算法会多次在数组中移动,比较相邻的数据,当左侧值大于右侧值时将它们进行互换。
16441647
@@ -1674,6 +1677,196 @@ console.log(mynums.toString);
16741677
16751678` ` `
16761679
1680+ ## 09 - 01
1681+ ### JS 基本应用- 函数
1682+
1683+ 1. 函数声明与函数表达式、对象实例化的区别
1684+
1685+ ` ` ` JavaScript
1686+ add1(1,1);
1687+ add2(1,2);
1688+ add3(1,3);
1689+ function add1(i, j){
1690+ console.log(i+j);
1691+ }
1692+ var add2 = function(i, j){
1693+ console.log(i+j);
1694+ }
1695+ var add3 = new Function("i", "j", "console.log(i+j);");
1696+ ` ` `
1697+
1698+ 2. 对象实例化与函数声明与函数表达式的区别
1699+
1700+ ` ` ` JavaScript
1701+ (function(){
1702+ var i = 10;
1703+ function add(j) {
1704+ console.log(i+j);
1705+ debugger;
1706+ }
1707+ add(1);
1708+ })();
1709+
1710+ (function(){
1711+ var i = 10;
1712+ var add = new Function("j", "console.log(i+j);debugger;");
1713+ add(1);
1714+ })();
1715+ ` ` `
1716+
1717+ 3. bind的使用
1718+
1719+ ` ` ` JavaScript
1720+ function Point(x, y){
1721+ this.x = x;
1722+ this.y = y;
1723+ }
1724+ Point.prototype.move = function(x, y) {
1725+ this.x += x;
1726+ this.y += y;
1727+ }
1728+ var p = new Point(0,0);
1729+ var circle = {x:1, y:1, r:1};
1730+ var circleMove = p.move.bind(circle, 2, 1);
1731+ circleMove();
1732+ ` ` `
1733+
1734+ 4. 构造函数
1735+
1736+ ` ` ` JavaScript
1737+ function Car(type,color){
1738+ this.type = type;
1739+ this.color = color;
1740+ this.status = "stop";
1741+ this.light = "off";
1742+ }
1743+ Car.prototype.start = function(){
1744+ this.status = "driving";
1745+ this.light = "on";
1746+ console.log(this.type + " is " + this.status);
1747+ }
1748+ Car.prototype.stop = function(){
1749+ this.status = "stop";
1750+ this.light = "off";
1751+ console.log(this.type + " is " + this.status);
1752+ }
1753+ var audi = new Car("audi", "silver");
1754+ var benz = new Car("benz", "black");
1755+ var ferrari = new Car("ferrari", "yellow");
1756+ ` ` `
1757+
1758+ 5. 函数调用模式
1759+
1760+ ` ` ` JavaScript
1761+ function add(i, j){
1762+ return i+j;
1763+ }
1764+ var myNumber = {
1765+ value: 1,
1766+ double: function(){
1767+ var helper = function(){
1768+ this.value = add(this.value,this.value);
1769+ }
1770+ helper();
1771+ }
1772+ }
1773+ ` ` `
1774+
1775+ 6. arguments 转数组
1776+
1777+ ` ` ` JavaScript
1778+ function add(i, j) {
1779+ var args = Array.prototype.slice.apply(arguments);
1780+ args.forEach(function(item){
1781+ console.log(item);
1782+ })
1783+ }
1784+ add(1,2,3);
1785+ ` ` `
1786+
1787+ 7. arguments .callee 使用
1788+
1789+ ` ` ` JavaScript
1790+ console.log(
1791+ (function(i){
1792+ if (i==0) {
1793+ return 1;
1794+ }
1795+ return i*arguments.callee(i-1);
1796+ })(5)
1797+ );
1798+ ` ` `
1799+
1800+ 8. 递归
1801+
1802+ ` ` ` JavaScript
1803+ function factorial(i){
1804+ if (i==0) {
1805+ return 1;
1806+ }
1807+ return i*factorial(i-1);
1808+ }
1809+ ` ` `
1810+
1811+ 9. 普通递归函数跟记忆函数调用次数对比
1812+
1813+ ` ` ` JavaScript
1814+ var factorial = (function(){
1815+ var count = 0;
1816+ var fac = function(i){
1817+ count++;
1818+ if (i==0) {
1819+ console.log('调用次数:' + count);
1820+ return 1;
1821+ }
1822+ return i*factorial(i-1);
1823+ }
1824+ return fac;
1825+ })();
1826+ for(var i=0;i<=10;i++){
1827+ console.log(factorial(i));
1828+ }
1829+ ` ` `
1830+
1831+ 10. 记忆函数
1832+
1833+ ` ` ` JavaScript
1834+ var factorial = (function(){
1835+ var memo = [1];
1836+ var count = 0;
1837+ var fac = function(i){
1838+ count++;
1839+ var result = memo[i];
1840+ if(typeof result === 'number'){
1841+ console.log('调用次数:' + count);
1842+ return result;
1843+ }
1844+ result = i*fac(i-1);
1845+ memo[i] = result;
1846+ return result;
1847+ }
1848+ return fac;
1849+ })();
1850+ for(var i=0;i<=10;i++){
1851+ console.log(factorial(i));
1852+ }
1853+ ` ` `
1854+
1855+ 11. curry函数柯里化
1856+
1857+ ` ` ` JavaScript
1858+ function add(value){
1859+ var helper = function(next){
1860+ value = typeof(value)==="undefined"?next:value+next;
1861+ return helper;
1862+ }
1863+ helper.valueOf = function(){
1864+ return value;
1865+ }
1866+ return helper
1867+ }
1868+ ` ` `
1869+
16771870-- -
16781871
16791872### JSDemo JS 小程序
0 commit comments