js
放在body
元素中頁面內容的後面。(1)defer
(2)async
(3)動態建立<script>標籤
(4)ajax
前端
(1)增長可維護性
(2)可緩存,加快頁面加載速度
(3)適應將來ajax
(1)第一個字母必須是一個字母,下劃線,或者一個美圓符號
(2)其餘字符能夠是字母,下劃線,美圓符號,或者數組json
定義:它定義了一種不一樣的解析和執行模型。
做用:
(1)不肯定的行爲將獲得處理
(2)對某些不安全的操做會拋出錯誤跨域
1四、break 終止循環,continue 跳過當前循環,繼續執行下個循環 ###數組
fuction person(name, age, job) { var o = new Object(); o.name = name; o.age = age; o.job = job; o.sayName = function() { alert(this.name) } return o; } var p1 = person()
function Person(name, age, job) { this.name = name; this.age = age; this.job = job; this.sayName = function() { alert(this.name) } } p1 = new Person()
和工廠方式的區別:
(1) 沒有顯式的建立對象;
(2)直接將屬性和方法賦給你了this對象;
(3)沒有reutrn 語句
建立實例的步驟:
(1)建立一個對象;
(2)講構造函數的做用域賦給新對象;
(3)執行構造函數中的代碼;
(4)返回新對象瀏覽器
function Person() {}; Person.prototype = { constructor: Person; nage = "bug"; age: 19; job: "coder"; sayName: function() { alert(this.name) } } var p1 = new Person()
原型模式的缺點:
(1)相同的屬性值;
(2)實例被共享緩存
funcition Person(name, age, job) { this.name = name; this.age = age; this.job = job; this.friends: ['a', 'b']; } Person.prototype.sayName = function() { alert(this.name) } var p1=new Person()
function Person(name, age, job) { this.name = name; this.age = age; this.job = job; if (typeof this.sayName != "function") { Person.prototype.sayName = function() { alert(this.name) } } } var p1 = new Person()
funcition Person(name,age,job){ var o=new Object(); o.name=name; o.age=age; o.job=job; o.sayName=function(){ alert(this.name } return o; } var p1= new Person()
function Person(name, age, job) { var o = new Object(); o.sayName = function() { alert(name) } return o; } var p = Person()
function SuperType() { this.property = true; } SuperType.prototype.getSuperValue = function() { return this.property; } function subType() { this.subproperty = false; } SubType.prototype = new SuperType(); SubType.prototype.getsubValue = function() { return this.subproperty; } var instance = new subType();
function SuperType() { this.colors = ["red", "blue", "green"]; } function SubType() { SuperType.call(this) } var instance1 = new subType();
function SuperType(name) { this.name = name; this.colors = ["red", "blue", "green"]; } SuperType.prototype.sayName = function() { alert(this.name) } function SubType(name, age) { SuperTyep.call(this, name) this.age = age; } SubType.prototype = new SuperType(); SubType.prototype = construtor = SubType; subTpye.prototype.sayAge = function() { alert(this.age) } var instance1 = new SubType('bug', 30)
function object(o) { function F() {} F.prototype = o; return new F(); }
function creatAnother(original) { var clone = object(original); clone.sayHi = function() { alert("hi") } return clone; }
function SuperType(name) { this.name = name; this.colors = ["red", "blue", "green"]; } SuperType.prototype.sayName = function() { alert(this.name); } function SubType(name, age) { SuperType.call(this, name); //第二次調用SuperType() this.age = age; } SubType.prototype = new SuperType(); //第一次調用SuperType() SubType.prototype.sayAge = function() { alert(this.age); }
function fun() { var result = []; for (var i = 0; i < 10; i++) { result[i] = function() { return i; } } return reslut }
每一個函數都返回10;安全
function fun() { var reslut = []; for (var i = 0; i < 10; i++) { result[i] = function(num) { return function() { return num; } }(i) } return result; }
返回1-10服務器
(1)在全局函數中,this等於window,
(2)做爲對象的方法調用時,this等於那個對象。閉包
var name = "window"; var object = { name: "object"; getName: function() { return function() { return this.name } } } alert(object.getName()()) // window var name = "window"; var object = { name: "object"; getName: function() { var _this = this; return function() { return _this.name } } } alert(object.getName()()) // object
(function(){ })()
IE: trident內核
Firefox:gecko內核
Chrome:Blink內核
一、location 對象
二、navigator 對象
三、history 對象
appendChild 添加
inserBefore 插入
replaceChild 替換
removeChild 刪除
一、querySelector()
二、querySelectorAll()
三、getElementsByClassName()
4.classList 屬性
(1)add()
(2)contains()
(3)remove()
(4)toggle()
五、children 屬性
ajax:頁面無刷新請求操做
get 用於想服務器查詢信息
post 用於向服務器發送信息
跨瀏覽器的 CORS
function CORS(method, url) { var xhr = new XMLHttpRequest(); if ("withCredentials" in xhr) { xhr.open(method, url, true) } else if (typeof XDomainRequset != "undefined") { xhr = new XDomainRequest(); xhr.open(method, url) } else { xhr = null; } reutrn xhr; } var req = CORS("get", "wwww")
定義:動態建立script標籤,回調函數