繁体   English   中英

javascript-将原型应用于非构造函数有什么作用?

[英]javascript - what is the effect of applying prototype to functions that are not constructors?

因此,我知道如何应用prototype属性修改将有效影响实例的构造函数。 从理论上讲,原型也应该在正常函数上工作(尽管如果不是特定实例的原型,则不一定会起作用)。 所以我尝试了代码(createNewFunction是不是构造函数的普通函数)(完整的代码在最后)

createNewPerson.prototype.xx = (function() { //alerts hello
            alert("hello");
        })();   

我想知道这实际上对xx附加到函数的作用。 它是否存储为等于anon函数的变量xx? 还是存储为this.xx =函数? 与存储以下代码相比,这有何不同:

createNewPerson.xx = (function() { //alerts hello
        alert("hello");
    })();

这也让我想知道如何存储此行(例如,是否将其存储为var mm = 3,或者根本不会追加?):

createNewPerson.mm = 3;

完整代码供参考:

 function createNewPerson(name) { var obj = {}; obj.name = name; obj.greeting = function() { alert('Hi! I\\'m ' + this.name + '.'); }; return obj; } createNewPerson.mm = 3; //does nothing when i tested it createNewPerson.xx = (function() { //alerts hello alert("hello"); })(); createNewPerson.prototype.xx = (function() { //alerts hello alert("hello"); })(); var salva = createNewPerson('Salva'); 

.prototype只是任何其他对象的对象属性。 函数默认情况下具有一个,并且当您以某些方式(例如,通过使用构造函数)创建对象时,可以通过特殊方式间接访问该.prototype属性。 但是,属性本身或为其分配任何东西都没有任何神奇之处。

createNewPerson.prototype.xx = (function() { //alerts hello
    alert("hello");
})();   

.prototype “ hello”发出警报,并将undefined的值分配给.prototype属性的.xx属性。

它是否存储为等于anon函数的变量xx?

它存储为等于undefined的属性xx

createNewPerson.xx = (function() { //alerts hello
    alert("hello");
})();

.xx “ hello”发出警报,并将undefined的值分配给函数本身的.xx属性。

这也让我想知道如何存储此行(例如,是否将其存储为var mm = 3,或者根本不会追加?):

createNewPerson.mm = 3;

我真的不明白您在这里说的是什么,但是所有要做的就是将值3分配给函数本身的.mm属性。

函数是对象,可以具有属性。

 function createNewPerson(name) { var obj = {}; obj.name = name; obj.greeting = function() { alert('Hi! I\\'m ' + this.name + '.'); }; return obj; } createNewPerson.mm = 3; //assigns 3 to createNewPerson property mm createNewPerson.xx = (function() { //assigns result of anonymous function to property xx alert("hello"); })(); createNewPerson.prototype.xx = (function() { //assigns result of function to xx alert("hello"); })(); var salva = createNewPerson('Salva');//creates new generic object alert(// these are values of the function, not the object salva 'mm: ' + createNewPerson.mm + '\\nxx: ' + createNewPerson.xx ); //To add a function to a function: createNewPerson.foo = function(){ alert('I\\'m just a function hanging off another function. I know nothing about the function I hang on.'); }; alert('the above code didn\\'t alert, but we can alert it now'); createNewPerson.foo(); 

将新成员附加到功能上不会给他们特殊的访问权限。

更新:因为当您调用createNewPerson('Salva'); 上下文是“窗口”,到达mm的唯一方法是引用createNewPerson.mm;

如果您正在寻找诸如“ this.mm”之类的较短内容,则可以更新代码以绑定上下文。 我添加了更新的代码来反映这一点。

 var createNewPerson = (function (name) { alert(this.prior); alert(this.after); var obj = { name: name }; return obj; }); createNewPerson.prior = 'prior to binding'; createNewPerson=createNewPerson.bind(createNewPerson); //this makes the createNewPerson its own context when it is called. createNewPerson.after = 'after to binding';//property is hung from the function instance created with "bind" var person = createNewPerson('salva'); 

似乎您正在寻找js构造函数语法; 不只是将属性分配给函数。

 //function with a property function foo(){ } foo.prop = true; alert(foo.prop); //maybe you want a constructor? function constructor(id) { this.id = id; } var thing = new constructor(32); alert(thing.id); 

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM