isam2016 / underscore Goto Github PK
View Code? Open in Web Editor NEWlearn underscore
learn underscore
/**
原型和原型链
参考王福朋博客
http://www.cnblogs.com/wangfupeng1988/tag/%E5%8E%9F%E5%9E%8B/
*/
一: 略
二: 函数和对象的关系
函数是对象的一种,对象都是函数创建的。eg:
function Employee(name,job,born)
{
this.name=name;
this.job=job;
this.born=born;
}
var bill=new Employee("Bill Gates","Engineer",1985);
三:prototype 原型
原型和函数的关系已经在开篇已经交代了
四:proto 隐式原型
每个对象都有一个隐式的属性,但是有些浏览器是不会让你发现的
文字版: 每个对象都有一个__proto__属性,指向创建该对象的函数的prototype。
eg: bill.__proto__ === Employee.protitype //true
eg: var obj={}
obj.__proto__ === Object.prototype //true
Object.prototype.__proto__ === null //true
最终的__proto__ 都要指向 Object.prototype(考虑Object.prototype.__proto__)
函数是特殊的对象,他当然也是有__proto__
Object.__proto__ === Functon.prototype
五: instanceof 运算
Instanceof运算符的第一个变量是一个对象,暂时称为A;第二个变量一般是一个函数,暂时称为B。
运算规则:沿着A的__proto__ 这条线来找,同时沿着B 的prototype 这条线来找,如果两条线能同
时找到同一个对象,那么就返回true,如果找到终点还没有找到,则返回false
console.log(Object instanceof Function) //true
console.log(Function instanceof Object) //true
console.log(Function instanceof Function) //true
console.log(Object instanceof Object) //true
instanceof表示的就是一种继承关系,或者原型链的结构。
六: 继承
javescript中的继承是通过原型链来体现的:
访问一个对象的属性时,先在基本属性中查找,如果没有,再沿着__proto__这条链向上找,这就是原型链。
由于所有的对象的原型链都会找到Object.prototype,因此所有的对象都会有Object.prototype的方法。这就是所谓的“继承”。
七:
//IIFE 将window穿入进去
//JS中(function(){xxx})()
/*
包围函数(function(){})的第一对括号向脚本返回未命名的函数,随后一对空括号立即执行返回的未命名函数,括号内为匿名函数的参数。
其他写法
(function () { code } ());
!function () { code } ();
~function () { code } ();
-function () { code } ();
+function () { code } ();
函数声明: function name(){}
函数表达式:var fnname = function(){}
匿名函数: function(){} 匿名函数属于函数表达式
两者最主要的区别是: 一: 变量提升
二: 函数表达式可以在后面添加扩看,但是命名函数不行吧
function fnName(){
alert('Hello World');
}();//error
function(){
console.log('Hello World');
}();//error 没有赋值
而()、!、+、-、=等运算符,都将函数声明转换成函数表达式,
消除了javascript引擎识别函数表达式和函数声明的歧义,告诉
javascript引擎这是一个函数表达式,不是函数声明,可以在后面
加括号,并立即执行函数的代码。
(function(a){
console.log(a); //firebug输出123,使用()运算符
})(123);
(function(a){
console.log(a); //firebug输出1234,使用()运算符
}(1234));
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.