您好,欢迎来到三六零分类信息网!老站,搜索引擎当天收录,欢迎发信息
三六零分类信息网 > 十堰分类信息网,免费分类信息发布

详解JS原型和原型链(三)

2025/9/26 5:08:16发布39次查看
这次接着上篇跟小编一起来看看js原型和原型链以及注意事项有哪些,下面就是实战案例,一起来看一下。
七. 函数对象 (复习一下前面的知识点)
所有函数对象的proto都指向function.prototype,它是一个空函数(empty function)
number.__proto__ === function.prototype // true number.constructor == function //true boolean.__proto__ === function.prototype // true boolean.constructor == function //true string.__proto__ === function.prototype // true string.constructor == function //true // 所有的构造器都来自于function.prototype,甚至包括根构造器object及function自身 object.__proto__ === function.prototype // true object.constructor == function // true // 所有的构造器都来自于function.prototype,甚至包括根构造器object及function自身 function.__proto__ === function.prototype // true function.constructor == function //true array.__proto__ === function.prototype // true array.constructor == function //true regexp.__proto__ === function.prototype // true regexp.constructor == function //true error.__proto__ === function.prototype // true error.constructor == function //true date.__proto__ === function.prototype // true date.constructor == function //true
javascript中有内置(build-in)构造器/对象共计12个(es5中新加了json),这里列举了可访问的8个构造器。剩下如global不能直接访问,arguments仅在函数调用时由js引擎创建,math,json是以对象形式存在的,无需new。它们的proto是object.prototype。如下
math.__proto__ === object.prototype // true math.construrctor == object // true json.__proto__ === object.prototype // true json.construrctor == object //true
上面说的函数对象当然包括自定义的。如下
// 函数声明 function person() {} // 函数表达式 var perosn = function() {} console.log(person.__proto__ === function.prototype) // true console.log(man.__proto__ === function.prototype) // true
这说明什么呢?
** 所有的构造器都来自于 function.prototype,甚至包括根构造器object及function自身。所有构造器都继承了·function.prototype·的属性及方法。如length、call、apply、bind**
(你应该明白第一句话,第二句话我们下一节继续说,先挖个坑:))
function.prototype也是唯一一个typeof xxx.prototype为 function的prototype。其它的构造器的prototype都是一个对象(原因第三节里已经解释过了)。如下(又复习了一遍):
console.log(typeof function.prototype) // function console.log(typeof object.prototype) // object console.log(typeof number.prototype) // object console.log(typeof boolean.prototype) // object console.log(typeof string.prototype) // object console.log(typeof array.prototype) // object console.log(typeof regexp.prototype) // object console.log(typeof error.prototype) // object console.log(typeof date.prototype) // object console.log(typeof object.prototype) // object
噢,上面还提到它是一个空的函数,console.log(function.prototype) 下看看(留意,下一节会再说一下这个)
知道了所有构造器(含内置及自定义)的__proto__都是function.prototype,那function.prototype的__proto__是谁呢?
相信都听说过javascript中函数也是一等公民,那从哪能体现呢?如下
console.log(function.prototype.__proto__ === object.prototype) // true
这说明所有的构造器也都是一个普通 js 对象,可以给构造器添加/删除属性等。同时它也继承了object.prototype上的所有方法:tostring、valueof、hasownproperty等。(你也应该明白第一句话,第二句话我们下一节继续说,不用挖坑了,还是刚才那个坑;))
最后object.prototype的proto是谁?
object.prototype.__proto__ === null // true
已经到顶了,为null。(读到现在,再回过头看第五章,能明白吗?)
八. prototype
在 ecmascript 核心所定义的全部属性中,最耐人寻味的就要数 prototype 属性了。对于 ecmascript 中的引用类型而言,prototype 是保存着它们所有实例方法的真正所在。换句话所说,诸如 tostring()和 valuseof() 等方法实际上都保存在 prototype 名下,只不过是通过各自对象的实例访问罢了。
——《javascript 高级程序设计》第三版 p116
我们知道 js 内置了一些方法供我们使用,比如:
对象可以用 constructor/tostring()/valueof() 等方法;
数组可以用 map()/filter()/reducer() 等方法;
数字可用用 parseint()/parsefloat()等方法;
why ???
当我们创建一个函数时:
var person = new object()
person 是 object 的实例,所以 person 继承了object 的原型对象object.prototype上所有的方法:
object.prototype
object 的每个实例都具有以上的属性和方法。
所以我可以用 person.constructor 也可以用 person.hasownproperty。
当我们创建一个数组时:
var num = new array()
num 是 array 的实例,所以 num 继承了array 的原型对象array.prototype上所有的方法:
array.prototype
are you f***ing kidding me? 这尼玛怎么是一个空数组???
我们可以用一个 es5 提供的新方法:object.getownpropertynames
获取所有(包括不可枚举的属性)的属性名不包括 prototy 中的属性,返回一个数组:
var arrayallkeys = array.prototype; // [] 空数组// 只得到 arrayallkeys 这个对象里所有的属性名(不会去找 arrayallkeys.prototype 中的属性)console.log(object.getownpropertynames(arrayallkeys));
/* 输出:
["length", "constructor", "tostring", "tolocalestring", "join", "pop", "push",
"concat", "reverse", "shift", "unshift", "slice", "splice", "sort", "filter", "foreach",
"some", "every", "map", "indexof", "lastindexof", "reduce", "reduceright",
"entries", "keys", "copywithin", "find", "findindex", "fill"]
*/
这样你就明白了随便声明一个数组,它为啥能用那么多方法了。
细心的你肯定发现了object.getownpropertynames(arrayallkeys) 输出的数组里并没有 constructor/hasownprototype等对象的方法(你肯定没发现)。
但是随便定义的数组也能用这些方法
var num = [1];console.log(num.hasownprototype()) // false (输出布尔值而不是报错)
why ???
因为array.prototype 虽然没这些方法,但是它有原型对象(__proto__):
// 上面我们说了 object.prototype 就是一个普通对象。 array.prototype.__proto__ == object.prototype
所以 array.prototype 继承了对象的所有方法,当你用num.hasownprototype()时,js 会先查一下它的构造函数 (array) 的原型对象 array.prototype 有没有有hasownprototype()方法,没查到的话继续查一下 array.prototype 的原型对象 array.prototype.__proto__有没有这个方法。
当我们创建一个函数时:
var f = new function("x","return x*x;"); //当然你也可以这么创建 f = function(x){ return x*x } console.log(f.arguments) // arguments 方法从哪里来的? console.log(f.call(window)) // call 方法从哪里来的? console.log(function.prototype) // function() {} (一个空的函数) console.log(object.getownpropertynames(function.prototype)); /* 输出 ["length", "name", "arguments", "caller", "constructor", "bind", "tostring", "call", "apply"] */
我们再复习第八小节这句话:
所有函数对象proto都指向 function.prototype,它是一个空函数(empty function)
嗯,我们验证了它就是空函数。不过不要忽略前半句。我们枚举出了它的所有的方法,所以所有的函数对象都能用,比如:
九. 复习一下
第八小节我们总结了:
所有函数对象的 __proto__ 都指向 function.prototype,它是一个空函数(empty function)
但是你可别忘了在第三小节我们总结的:
所有对象的 __proto__ 都指向其构造器的 prototype
我们下面再复习下这句话。
先看看 js 内置构造器:
var obj = {name: 'jack'} var arr = [1,2,3] var reg = /hello/g var date = new date var err = new error('exception') console.log(obj.__proto__ === object.prototype) // true console.log(arr.__proto__ === array.prototype) // true console.log(reg.__proto__ === regexp.prototype) // true console.log(date.__proto__ === date.prototype) // true console.log(err.__proto__ === error.prototype) // true
再看看自定义的构造器,这里定义了一个 person:
function person(name) { this.name = name; } var p = new person('jack') console.log(p.__proto__ === person.prototype) // true
p 是 person 的实例对象,p 的内部原型总是指向其构造器 person 的原型对象 prototype。
每个对象都有一个 constructor 属性,可以获取它的构造器,因此以下打印结果也是恒等的:
function person(name) { this.name = name } var p = new person('jack') console.log(p.__proto__ === p.constructor.prototype) // true
上面的person没有给其原型添加属性或方法,这里给其原型添加一个getname方法:
function person(name) { this.name = name } // 修改原型 person.prototype.getname = function() {} var p = new person('jack') console.log(p.__proto__ === person.prototype) // true console.log(p.__proto__ === p.constructor.prototype) // true
可以看到p.__proto__与person.prototype,p.constructor.prototype都是恒等的,即都指向同一个对象。
如果换一种方式设置原型,结果就有些不同了:
function person(name) { this.name = name } // 重写原型person.prototype = { getname: function() {} } var p = new person('jack') console.log(p.__proto__ === person.prototype) // true console.log(p.__proto__ === p.constructor.prototype) // false
这里直接重写了 person.prototype(注意:上一个示例是修改原型)。输出结果可以看出p.__proto__仍然指向的是person.prototype,而不是p.constructor.prototype。
这也很好理解,给person.prototype赋值的是一个对象直接量{getname: function(){}},使用对象直接量方式定义的对象其构造器(constructor)指向的是根构造器object,object.prototype是一个空对象{},{}自然与{getname: function(){}}不等。如下:
var p = {} console.log(object.prototype) // 为一个空的对象{} console.log(p.constructor === object) // 对象直接量方式定义的对象其constructor为objectconsole.log(p.constructor.prototype === object.prototype) // 为true, 不解释(๑ˇ3ˇ๑)
十. 原型链(再复习一下:)
下面这个例子你应该能明白了!
function person(){ }var person1 = new person(); console.log(person1.__proto__ === person.prototype); // true console.log(person.prototype.__proto__ === object.prototype) //true console.log(object.prototype.__proto__) //null person.__proto__ == function.prototype; //true console.log(function.prototype)// function(){} (空函数) var num = new array()console.log(num.__proto__ == array.prototype) // true console.log( array.prototype.__proto__ == object.prototype) // true console.log(array.prototype) // [] (空数组) console.log(object.prototype.__proto__) //null console.log(array.__proto__ == function.prototype)// true
疑点解惑:
object.__proto__ === function.prototype // true
object 是函数对象,是通过new function()创建的,所以object.__proto__指向function.prototype。(参照第八小节:「所有函数对象的__proto__都指向function.prototype」)
function.__proto__ === function.prototype // true
function 也是对象函数,也是通过new function()创建,所以function.__proto__指向function.prototype。
自己是由自己创建的,好像不符合逻辑,但仔细想想,现实世界也有些类似,你是怎么来的,你妈生的,你妈怎么来的,你姥姥生的,……类人猿进化来的,那类人猿从哪来,一直追溯下去……,就是无,(null生万物)
正如《道德经》里所说“无,名天地之始”。
function.prototype.__proto__ === object.prototype //true
其实这一点我也有点困惑,不过也可以试着解释一下。
function.prototype是个函数对象,理论上他的__proto__应该指向 function.prototype,就是他自己,自己指向自己,没有意义。
js一直强调万物皆对象,函数对象也是对象,给他认个祖宗,指向object.prototype。object.prototype.__proto__ === null,保证原型链能够正常结束。
十一 总结
原型和原型链是js实现继承的一种模型。
原型链的形成是真正是靠__proto__ 而非prototype
要深入理解这句话,我们再举个例子,看看前面你真的理解了吗?
var animal = function(){}; var dog = function(){}; animal.price = 2000; dog.prototype = animal; var tidy = new dog(); console.log(dog.price) //undefined console.log(tidy.price) // 2000
这里解释一下:
var dog = function(){}; dog.prototype.price = 2000; var tidy = new dog(); console.log(tidy.price); // 2000 console.log(dog.price); //undefined var dog = function(){}; var tidy = new dog(); tidy.price = 2000; console.log(dog.price); //undefined
这个明白吧?想一想我们上面说过这句话:
实例(tidy)和 原型对象(dog.prototype)存在一个连接。不过,要明确的真正重要的一点就是,这个连接存在于实例(tidy)与构造函数的原型对象(dog.prototype)之间,而不是存在于实例(tidy)与构造函数(dog)之间。
聪明的你肯定想通了吧 :)
以上就是详解js原型和原型链(三)的详细内容。
十堰分类信息网,免费分类信息发布

VIP推荐

免费发布信息,免费发布B2B信息网站平台 - 三六零分类信息网 沪ICP备09012988号-2
企业名录 Product