为什么在构造函数内部定义原型报错

问题描述

function fn(o){ arguments.callee.prorotype={ shout:function(){ alert('shout'); } } for(var i in o) this[i]=o[i]}var obj = new fn({name:'eric'});obj.shout();//error下面这样写就不报错:function fn(o){ for(var i in o) this[i]=o[i]}fn.prorotype={ shout:function(){ alert('shout'); }}var obj = new fn({name:'eric'});obj.shout();//shout

解决方案

你可以看看ECMAScript文档中的 Function -> Construct 这节(4版 13.2.2)对JS函数(假定为F)对象使用new操作符,进行构造(Construct)时,它的处理步骤如下:(翻译后的简化版,部分不常用分支移掉了)创建一个新的空对象o将o的原型([[prototype]])设定为F的prototype属性以o为this指针调用构造函数在你这里,创建obj的时候,在执行到fn内部的代码之前,它的[[prototype]]即原型链已设定完毕,在fn内部的修改不影响到obj了。你可以试试用这个:arguments.callee.prorotype.should = function(){}它修改了原型对象的属性,可以影响到obj附原版:When the [[Construct]] property for a Function object F is called, the following steps are taken:1. Create a new native ECMAScript object.2. Set the [[Class]] property of Result(1) to "Object".3. Get the value of the prototype property of the F.4. If Result(3) is an object, set the [[Prototype]] property of Result(1) to Result(3).5. If Result(3) is not an object, set the [[Prototype]] property of Result(1) to the original Objectprototype object as described in 15.2.3.1.6. Invoke the [[Call]] property of F, providing Result(1) as the this value and providing the argumentlist passed into [[Construct]] as the argument values.7. If Type(Result(6)) is Object then return Result(6).8. Return Result(1).

时间: 2024-08-29 22:54:46

为什么在构造函数内部定义原型报错的相关文章

processing-java 数组定义,报错:unexpected token: [ 如何解决?

问题描述 java 数组定义,报错:unexpected token: [ 如何解决? RT,代码如下: class Data { String name; int size; int level; int father; int x = 0; int y = 0; int w = 0; int h = 0; Data(String tname, int tsize, int tlevel, int tfather) { name = tname; size = tsize; level = t

struts取值问题-struts使用DomainModel取值为什么必须提供一个空构造函数否则就会报错?

问题描述 struts使用DomainModel取值为什么必须提供一个空构造函数否则就会报错? java.lang.NullPointerException com.neusoft.action.UserAction.execute(UserAction.java:19) sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) sun.reflect.NativeMethodAccessorImpl.invoke(NativeMet

分页-grid里面的stroe报错,求解决

问题描述 grid里面的stroe报错,求解决 如图!我要获取前台分页,然后我stroe报错了,我知道这个store没有定义的原因!但是我用var定义也报错啊!我的思路是应该是要获取grid里面的store,但是我不知道怎么获取啊!而且大家注意了,我的这个stroe是写在grid里面的,所以我var store = Ext.create('Ext.data.Store', {}会报错啊!一定是我打开的方式不对,求大伙告诉!谢谢啦 解决方案 http://zhidao.baidu.com/link

初学者求助!!!dispatcher.runasync的简单报错,懂的人举手之劳

问题描述 提示说System.windows.threading.dispatcher不包含runasync的定义AsRandomAccessStream也是,提示System.IO.Stream不包含AsRandomAccessStream的定义,但是这篇帖子里就是这么用的啊http://www.cnblogs.com/linzheng/p/3764315.html#commentform 解决方案 解决方案二:命名空间没引用?或缺少dll?解决方案三:引用1楼happy09li的回复: 命名

c++ 构造函数初始化报错如何解决

问题描述 c++ 构造函数初始化报错如何解决 #include #includeusing namespace std;class Teacher{public: Teacher(); Teacher(string nameint age) ;//如果对age进行初始化并且在main函数中实例化 一个 t5("Vivion")为什么会报错 Teacher(const Teacher&tea); void setName(string _name); string getName

如何定义一个类 编译时,使得第一行报错,第二行正确

问题描述 如何定义一个类Aint main(){ A a; //第一行 A *b=new A(); //第二行}如何定义一个类 编译时,使得第一行报错,第二行正确 问题补充:enet_java 写道 解决方案 [quote java]#include "stdio.h"class test{public:static test* getinstance(){return(new test());}int puthello(){puts("hello world");

php-laravel controller 里面定义以head开头的方法 报错

问题描述 laravel controller 里面定义以head开头的方法 报错 BadMethodCallException in Macroable.php line 81: Method head does not exist.

vs2013 报错-DoubleAnimation不包含采用“3”个参数的构造函数

问题描述 DoubleAnimation不包含采用"3"个参数的构造函数 Storyboard storyboard = new Storyboard(); DoubleAnimation doubleAnimation = new DoubleAnimation(Canvas.GetLeft(rect) p.X new Duration(TimeSpan.FromMilliseconds(500))); Storyboard.SetTarget(doubleAnimation rec

servlet内部跳转if语句不执行,也不报错

问题描述 servlet内部跳转if语句不执行,也不报错 } else if ("shoppingCartBooks".equals(flag)) { String username = req.getParameter("username"); System.out.println(username+"****"); if (username != null) { List list = bs.getAllBooksInShoppingCart