即使没有原型属性也可以将属性添加到对象,那么原型属性有什么用?
var o = {};
o.x = 5;
o.y = test;
test = new function(){ alert("hello"); };
Run Code Online (Sandbox Code Playgroud) 在http://jsfiddle.net/javascriptenlightenment/QvbDw/的代码片段中,作者使用2个新属性(数组属性和函数属性)扩充了内置String对象构造函数.
我注意到对于新的数组属性,他这样做了:
String.newArrayProperty = [];
// Q1: Why not String.prototype.newArrayProperty = []; ?
Run Code Online (Sandbox Code Playgroud)
但对于新的功能属性,他这样做:
String.prototype.newFunctionProperty = function() {...};
// Q2: Why not String.newFunctionProperty = function() {...}; ?
Run Code Online (Sandbox Code Playgroud)
String.newProperty和String.prototype.newProperty有什么区别?
function Demo() {
this.show1 = function() { alert(1) }
}
Demo.prototype.show2 = function() { alert(2) }
var d = new Demo
d.show1()
d.show2()
Run Code Online (Sandbox Code Playgroud)
show1并且show2都可以提醒号码.
这两者有什么区别吗?
如何从jquery对象中删除函数?例如,我有
$('#element').func = function() {
// function body goes here
}
Run Code Online (Sandbox Code Playgroud)
而后来我想删除func的$('#element').我试过delete但它不起作用.谢谢.
我正在学习JavaScript并读取函数就像对象,并且可以设置如下属性:
var person = function(){
}
person.name="John Smith"; //output ""
person.age=21; //output 21
person.profession="Web Developer"; //output "Web Developer"
Run Code Online (Sandbox Code Playgroud)
为什么name属性为空?
谢谢
我有这个代码:
function user(name) {
console.log(name);
}
user.prototype.test = function() {
return 2 + 2;
};
console.log(user.prototype.test());
var dany = new user("dany");
var david = new user("david");
console.log(dany.prototype.test());
Run Code Online (Sandbox Code Playgroud)
控制台日志:
4
dany
david
Uncaught TypeError: Cannot call method 'test' of undefined
Run Code Online (Sandbox Code Playgroud)
不应该将test()函数分配给user()函数的所有实例(它是对象构造函数)吗?
如果您碰巧对我应该阅读的内容有更好的建议,请更多地了解原型,请继续;)
编辑:
即使使用:
Object.prototype.test = function() {
return 2 + 2;
};
Run Code Online (Sandbox Code Playgroud)
我仍然在控制台中收到该错误.我以为所有对象都会继承原型函数.
我是JS的新手,我正在尝试理解chartAt.我创建了一个问题,我想通过一个数组,并使用charAt从我的数组中拉出每个值的第一个字符.我的代码有点困难.
var myArray = ['adam', 'bianca', 'cat', 'dennis'];
var myFunc = function (letter) {
for (var i = 0; i < letter.length; i += 1) {
letter.charAt(0);
console.log(letter.charAt(0));
}
}
Run Code Online (Sandbox Code Playgroud) 我正在使用jquery对一个PHP脚本进行ajax调用,该脚本将json编码的字符串作为对象发回.
{
"errors":{
"name":"Name is empty",
"gender":"Gender is empty"
},
"success":false,
"message":"FAIL"
}
Run Code Online (Sandbox Code Playgroud)
这是json返回的,因为我dataType: 'json'在我的ajax调用中使用它,它被转换为一个对象,然后我有控制台记录观察.
我看到键/值对在那里,但也有一些(原型)不是json字符串的一部分.它是什么,来自哪里?我可以使用这些功能吗?
我正在附上一个截图,因为输入会弄乱它.
无论我使用"this.value = 1;",我都会得到完全相同的结果 在构造函数内部或只是将属性值放在函数构造函数的原型中 - "MyClass.prototype.value = 1;"
function MyClass() {
//this.value=1;
}
MyClass.prototype.value =1;
var a = new MyClass();
document.write(a.value + "<br>");
a.value=13;
document.write(a.value + "<br>");
var b = new MyClass();
document.write(b.value);
Run Code Online (Sandbox Code Playgroud)
结果是:
1
13
1
因为最后一个值是1,显然每个对象(a,b)都在它自己的内存块中获得它自己的值副本,那么如果它们不在对象之间共享,究竟是什么原型值的使用呢?
我在原型之间感到困惑.我已经研究并观看了教程,但我仍未能得到明确的答案.很高兴,如果有人可以帮助我.如果使用一些简单的例子或解释很好.
Prototype是库吗?例如Jquery
如是.这意味着我们需要在使用它之前将其添加到我们的文件中.就像我们将Jquery添加到头部,然后我们可以访问它的所有功能.
因此我们需要在使用它之前学习它,因为原型是使用像Jquery这样的纯javascript构建的.
如果Prototype是一个库,那么我们如何在不添加文件的情况下访问它?
例如: - 当我们编写一些javascript代码时,我们会自动访问Prototype的内容,如下面的代码所示.
function Apple (type) {
this.type = type;
this.color = "red";
}
Run Code Online (Sandbox Code Playgroud)
Apple.prototype.getInfo = function(){return this.color +''+ this.type +'apple'; };
有人说Prototype实际上是一个Javascript.
如果这是正确的,那么我们如何在JSFiddle下面的列表中分离原型和jQuery.

或者上面的图像中的Prototype库是否与Javascript原型对象不同?
意味着这些是两件不同的事情.
能否请你澄清我的这4点.
谢谢.