我正在努力理解javascript中的函数和对象.据说,函数也是对象,对象是一种"关联数组",即键值对的集合.我明白,如果我写
function myFunction() {
var value = 0;
}
alert(myFunction.value); //then this gives me "undefined"
Run Code Online (Sandbox Code Playgroud)
因为变量有功能范围.但如果我写
function myFunction() {
this.value = 0;
}
alert(myFunction.value); //then this gives me "undefined" too.
Run Code Online (Sandbox Code Playgroud)
但最后,如果我写
function myFunction() {
this.value = 0;
}
myFunction.value = 0;
alert(myFunction.value); //then this gives me 0
Run Code Online (Sandbox Code Playgroud)
所以我可以给myFunction属性"value"但是来自"outside".有人可以解释发生了什么以及为什么this.value = 0; 不会创造属性"价值".
当选择使用这四种模式中的一种而不是其他模式时,是否存在任何重要/细微/显着的差异?并且,当通过Object.create()vs new运算符"实例化"时,它们之间是否存在差异?
1)CoffeeScript翻译"类"定义时使用的模式:
Animal = (function() {
function Animal(name) {
this.name = name;
}
Animal.prototype.move = function(meters) {
return alert(this.name + (" moved " + meters + "m."));
};
return Animal;
})();
Run Code Online (Sandbox Code Playgroud)
和
2)Knockout似乎促进的模式:
var DifferentAnimal = function(name){
var self = this;
self.name = name;
self.move = function(meters){
return alert(this.name + (" moved " + meters + "m."));
};
}
Run Code Online (Sandbox Code Playgroud)
和
3)我经常看到的类似的简单模式:
var DifferentAnimalWithClosure = function(name){
var name = name;
var move = …Run Code Online (Sandbox Code Playgroud) 我有这个功能:
function Foo(){}
根据这张图片:

>> Foo.prototype -> Foo {}
Run Code Online (Sandbox Code Playgroud)

所以,如果我写:
Foo.prototype.constructor
现在 - 它引用了创建 对象的构造函数Foo{}function Foo(){}

一切都好.
那问题在哪里?
Foo.prototype 是一个对象实例 - > Foo {}
谁是构造函数Foo{}?
它是 function Foo(){}
你可以在这里看到黄色:

好
但我在这里被告知:

因此,Foo.prototype.__proto__应该参考prototype的function Foo(){} ,而不是Object.prototype!
如果我翻译它:
__proto__ is the ctor's prototype
_________________________________________________________________________________________
Foo.prototype.__proto__ is the function Foo(){} Foo.prototype
Run Code Online (Sandbox Code Playgroud)
但这是错误的
我可能错了,因为:

我错过了什么?
如果我执行以下操作(在全局范围内):
var myObject = {name: "Bob"};
Run Code Online (Sandbox Code Playgroud)
我有办法在内存中指向该对象(即字符串标识符"myObject)".我可以打开控制台并输入:myObject.name控制台将响应:
"Bob"
Run Code Online (Sandbox Code Playgroud)
现在,如果我输入:
{name: "Jane"};
Run Code Online (Sandbox Code Playgroud)
我正在某个地方创造那个物体,我猜它继续生活在某个范围内.有什么方法可以找到它吗?它是否存在window于某些通用商店的某个地方?
编辑:有人说它只会收集垃圾.
那么这个例子怎么样:
var MyObject = function(){
$("button").click(this.alert);
}
MyObject.prototype.alert = function(){
alert("I heard that!")
}
new MyObject();
Run Code Online (Sandbox Code Playgroud)
它不能被垃圾收集,因为它的回调绑定到DOM事件.生成的对象在哪里存在并且可以访问它?
如何从该类的实例生成该类的新实例?
考虑下一个类及其实例:
// Create a new class
var Foo = function Foo () {
console.log('New instance of FOO!');
};
Foo.prototype.generate = function(first_argument) {
this.arr = [1,2,3,4,5];
};
var foo = new Foo();
foo.generate();
console.log(foo.arr); // [1,2,3,4,5]
Run Code Online (Sandbox Code Playgroud)
请注意,它foo是一个实例 fo Foo,我将在示例中使用该实例。
使用 Object.create 我可以生成 foo 实例的新副本,但它们将共享状态数据:
var bar = Object.create(foo);
console.log(bar.arr); // [1,2,3,4,5]
bar.arr.push(6);
console.log(foo.arr); // [1,2,3,4,5,6]
console.log(bar.arr); // [1,2,3,4,5,6]
Run Code Online (Sandbox Code Playgroud)
如果某些逻辑位于构造函数中,则不会调用它。
这与对象创建类似,但它调用构造函数。它仍然存在相同的状态数据问题:
var Bar = function () {
foo.constructor.call(this);
};
Bar.prototype = foo;
var bar …Run Code Online (Sandbox Code Playgroud) 我知道这是相当直接使用jQuery但是对于大学作业需要插入新段落点击更多链接然后删除它们点击较少的链接 - 我们不使用CSS或jQuery所以我的代码到目前为止看起来像这样 - 插入工作但删除less()函数没有任何想法为什么(甚至尝试简单的警报从少功能和一个href上的返回false不能正常工作重定向页面到没有javascript默认.
window.onload= function()
{
var href = document.getElementById("more");
href.setAttribute("onclick","more(); return false;");
var more = document.getElementById("more");
more.onclick = function more()
{
var para1 = document.createElement("p");
para1.setAttribute("id", "para1");
var para1Cont = document.createTextNode("my text block 1");
para1.appendChild(para1Cont);
var more = document.getElementById("more");
more.parentNode.insertBefore(para1,more);
var para2 = document.createElement("p");
para2.setAttribute("id", "para2");
var para2Cont = document.createTextNode("My text block 2");
para2.appendChild(para2Cont);
more.parentNode.insertBefore(para2,more);
var toLess = more.setAttribute("id", "less");
var less = document.getElementById("less");
less.setAttribute("onclick", "less(); return false;");
less.innerHTML ="click here for less";
return false;
};
var …Run Code Online (Sandbox Code Playgroud) 在JavaScript中,为什么以下两个都返回true?
> var a;
undefined
> "a" in window;
true
> a in window;
true
Run Code Online (Sandbox Code Playgroud)
是否存在某种类型强制,或者JavaScript是否存储a为字符串和窗口中的变量?
请随意重写这个问题的标题 - 我不确定如何描述这种令人困惑的现象.
我一直在查看my.class.js的源代码,以了解是什么让它在Firefox上如此之快.这是用于创建类的代码片段:
my.Class = function () {
var len = arguments.length;
var body = arguments[len - 1];
var SuperClass = len > 1 ? arguments[0] : null;
var hasImplementClasses = len > 2;
var Class, SuperClassEmpty;
if (body.constructor === Object) {
Class = function () {};
} else {
Class = body.constructor;
delete body.constructor;
}
if (SuperClass) {
SuperClassEmpty = function() {};
SuperClassEmpty.prototype = SuperClass.prototype;
Class.prototype = new SuperClassEmpty();
Class.prototype.constructor = Class;
Class.Super = SuperClass;
extend(Class, SuperClass, …Run Code Online (Sandbox Code Playgroud)