prototype/ constructor都是个什么鬼?

看了一大堆文章,晦涩难懂,是因为我还没有理解prototype和constructor的基本概念和计算机原理。So~

一、什么是protorype?

构造函数除了创建对象外,还有一个

二、什么是constructor?

构造函数(constructor)除了创建对象外,还有一个特殊的功能,就是自动为创建的新对象增加了原型对象(prototype object),原型对象存放在ConstructorFunction.prototype属性中。

时间: 2024-08-25 22:32:37

prototype/ constructor都是个什么鬼?的相关文章

JS 中 讨厌的 prototype constructor _pro_ 理解

这几天一直在看着块,哎,看的人头都大了,差不多明白了,总结一下. prototype:创建的每一个函数都有一个prototype属性,这个属性指向一个prototype对象.即他是通过构造函数而创建的实例对象的原型对象(属性存在于构造函数中). constructor:这个属性在原型对象中,指向函数中的prototype属性.(存在于原型对象中,原型对象===原型) _proto_:这个属相存在于实例中,指向原型. 举个栗子: function ljd(){ this.name="ljd&quo

原型模式Prototype,constructor,__proto__详解

最近由于在找工作,又拿起<JavaScript高级程序设计>看了起来,从中也发现了自己确实还是有很多地方不懂,刚刚看到原型模式这里,今天终于搞懂了,当然,我也不知道自己的理解是否有错. 1.prototype 开头第一句话说,我们每创建一个函数,就会有一个prototype属性,这个属性是一个指针,指向一个对象,而这个对象的用途是包含由特定类型或者实例共享的属性和方法. function fn(){}; console.log(fn.prototype); 其实 其实我们创建函数就是调用构造函

Object.prototype.__proto__ 、Object.prototype和 Object.prototype.constructor

Object.prototype.__proto__: 实体对象指向造它的构造函数的 prototype属性所指 的对象 ,实例的__proto__是引用构造函数的prototype属性所指对象, Object.prototype : js规定,构造函数有prototype 属性,指向一个对象,这个对象一般就是构造函数的公有成员. 所以 __proto__  和 prototype 可以指向同一个对象  {key1:value1,key2:value2,......},构造函数的私有和特权函数会

深入理解Javascript中this, prototype, constructor

在Javascript面向对象编程中经常需要使用到this,prototype和constructor这3个关键字. 1.首先介绍一下this的使用:this表示当前对象;如果在全局中使用this,则this为当前页面对象window;如果在函数中使用this,则this为调用该函数的对象;可以使用apply和call两个全局函数来改变this的指向. 接下来,首先通过几个demo程序验证一下: function testFunction(){ console.log(this.variable

prototype/constructor/__proto__之constructor。

1.constructor的字面意思就是构造.它是对象的一个属性,它对应的值是该对象的“构造者” 1 //一.构造函数实例化的对象的constructor 2 function Cmf(n,m){ 3 this.n=n; 4 this.m=m; 5 this.mn=function(){ 6 return this.n+'|'+this.m 7 } 8 } 9 var cmf=new Cmf('c',"q") 10 console.log(cmf.mn()) // c|q 11 con

js深入学习-js prototype constructor属性区别

在很多js 插件中出现这两个属性的频率很高,我自己写插件时,也用到过,知道用,不知道具体的区别,今天研究了下, constructor 返回的是对象(类型的实例)的构造函数,通过prototype 添加的属性和方法不会返回. prototype 返回的是类型的原型,不会饭后构造函数部分. 实例如下: <html> <head> <script type="text/javascript">    var cat=function (name,sex)

prototype/constructor/__proto__之prototype简单应用

一.简单使用构造原型加prototype造简单的轮子. 1.想jQ那样获取HTML元素,先看JS代码 1 function Cmf() { //创建构造函数 2 3 this.arry = [] 4 5 } 6 Cmf.prototype.getNode = function(tag, id) { //给构造函数定义一个getNode方法 7 if (tag.indexOf('.') == 0) { //如果传入的是类class形式 .nav 8 var oparent = id ? docum

prototype constructor __proto__

constructor, prototype, __proto__ 详解

prototype &amp; constructor

Examples: Displaying the constructor of an object The following example creates a prototype, Tree, and an object of that type. The example then displays the constructor property for the object theTree. function Tree(name) { this.name = name; } var th