这可能会让你觉得这是一个语法不正确和可能是疯狂的问题,但这就是我的意思:当试图理解prototypeJavaScript中的概念时,我遇到了以下几个或多或少复杂版本的例子:
//Guitar function constructor
function Guitar(color, strings) {
this.color = color;
this.strings = strings;
}
//Create a new instance of a Guitar
var myGuitar = new Guitar('Black', ['D', 'A', 'D', 'F', 'A', 'E']);
//Adding a new method to Guitar via prototype
Guitar.prototype.play = function (chord) {
alert('Playing chord: ' + chord);
};
//Now make use of this new method in a pre-declared instance
myGuitar.play('D5');
Run Code Online (Sandbox Code Playgroud)
所以,关于我的问题:为什么你想要这样做?你为什么不直接把这个play功能放进Guitar去?为什么声明一个实例然后开始添加方法?我能看到的唯一原因是,如果你想myGuitar在play最初创建它时无法访问它,但我可以想出没有一个例子来说明为什么你会想要这样的东西.
看起来这样做会更有意义:
function Guitar(color, string) { …Run Code Online (Sandbox Code Playgroud) 关于构造函数在Javascrpt中的工作原理,我感到非常困惑.尽管使用该语言已有好几年了(大多数情况下它就像是LISP的半命令版本),但我想更多地了解对象应该如何工作.
鉴于此代码:
function Foo(x) {
return {
bar: function() { return x; }
};
}
Run Code Online (Sandbox Code Playgroud)
打电话myFoo = Foo(5)和有myFoo = new Foo(5)什么区别?或者,换句话说,Javascript中的构造函数究竟是做什么的?
我刚刚开始考虑这个问题,但是在jsFiddle中乱窜时无法让自己暴露出来.
var a = new Array(1),
b = Array(1);
console.log(a, b);
Run Code Online (Sandbox Code Playgroud)
输出是两个具有一个未定义成员的数组.
做一个for ( in )揭示他们有相同的属性.
这些有什么区别?第一个是否只是显式地实例化对象?
请不要告诉我使用数组文字表示法,因为我已经知道了.我更希望在上面解释的知识中填补这个空白.
我是C#的新手,来自C++背景.在C++中,您可以这样做:
class MyClass{
....
};
int main()
{
MyClass object; // this will create object in memory
MyClass* object = new MyClass(); // this does same thing
}
Run Code Online (Sandbox Code Playgroud)
而在C#中:
class Program
{
static void Main(string[] args)
{
Car x;
x.i = 2;
x.j = 3;
Console.WriteLine(x.i);
Console.ReadLine();
}
}
class Car
{
public int i;
public int j;
}
Run Code Online (Sandbox Code Playgroud)
你不能这样做.我想知道为什么Car x不做它的工作.
我正在寻找一种简单的方法来创建两个类,一个继承自另一个类,子类重新定义父方法之一,并在新方法内调用父类.
例如,有一个类,Animal并且Dog,Animal类定义了一个makeSound()确定如何输出声音的方法,然后Dog在其自己的makeSound()方法中覆盖以产生"低音"声音,但同时也调用Animal makeSound()来输出那个低音.
我在这里查看了John Resig的模型,但它使用了arguments.calleeECMA脚本中明显折旧的本机属性.这是否意味着我不应该使用John Resig的代码?
使用Javascript的原型继承模型编写动物/狗代码的简洁方法是什么?
我想知道一旦它构建完成后我们还能改变它吗?
var O = function(someValue){
this.hello = function(){
return "hello, " + someValue;
}
}
O.prototype.hello = function(){
return "hhhhhhh";
}
var i = new O("chris");
i.hello(); // -> this still returns the old definition "hello, chris"
Run Code Online (Sandbox Code Playgroud)
javascript语句O.prototype.hello = function(){....}不会覆盖并重新定义hello函数行为.这是为什么 ?我知道如果你试图重用参数会有类型错误someValue.
// this will fail since it can't find the parameter 'someValue'
O.prototype.hello = function(){
return "aloha, " + someValue;
}
Run Code Online (Sandbox Code Playgroud)
我想知道为什么它允许在运行时添加功能
O.prototype.newFunction = function(){
return "this is a new function";
}
i.newFunction(); // print 'this …Run Code Online (Sandbox Code Playgroud) 重要说明 在正常项目中实例化Cesium对象时,它没有任何以下划线为前缀的属性(_dataSourceCollection,_dataSourceDisplay等).但是,在Drupal中实例化时,除了常用属性外,还在对象上设置了大约40-45个属性(所有属性都带有下划线).这发生在Drupal 7或8中,虽然我不确定这是否与我遇到的问题有关,但这是一个明显的差异因此我认为它应该被分享.
我已将Cesium库添加到Drupal项目中,方法是将文件放在sites/all/libraries/cesium/Cesium.js以及Assets和Widgets文件夹中,然后在自定义模块中调用hook_library_info
function cesium_library_info() {
$libraries['cesium'] = array(
'files' => array(
'js' => 'Cesium.js',
),
'path' => 'js',
'library path' => libraries_get_path('cesium'),
'version' => '1'
);
return $libraries;
}
Run Code Online (Sandbox Code Playgroud)
然后我使用hook_menu返回以下页面回调:
function cesium_page() {
drupal_add_js(libraries_get_path('cesium') . '/Cesium.js');
drupal_add_js(drupal_get_path('module', 'cesium') . '/js/mCesium.js');
drupal_add_css(libraries_get_path('cesium') . '/Widgets/widgets.css');
$page = array();
$page['ces-container'] = array(
'#prefix' => '<div id="myApp-cesium">',
'#suffix' => '</div>',
'#markup' => '<h1>Welcome to Cesium!',
);
return $page;
}
Run Code Online (Sandbox Code Playgroud)
mCesium.js包含从Drupal.behaviors中调用Cesium的代码,用于将Viewer附加到我的#myApp-cesium元素.
Drupal.behaviors.cesium = {
attach: function (context, settings) {
var viewer = new …Run Code Online (Sandbox Code Playgroud) 我理解行为上的差异.Date()返回表示当前日期的String,并new Date()返回我可以调用其方法的Date对象的实例.
但我不知道为什么.JavaScript是原型,因此Date是一个函数和一个具有成员函数(方法)的对象,它们也是对象.但我没有编写或阅读任何行为方式的JavaScript,我想了解其中的区别.
有人可以向我展示一个具有方法的函数的示例代码,使用new运算符返回一个实例,并在直接调用时输出一个String吗?即这样的事情是怎么发生的?
Date(); // returns "Fri Aug 27 2010 12:45:39 GMT-0700 (PDT)"
new Date(); // returns Object
new Date().getFullYear(); // returns 2010
Date().getFullYear(); // throws exception!
Run Code Online (Sandbox Code Playgroud)
非常具体的要求,我知道.我希望这是件好事.:)
我用这个样本回答了一个关于闭包的问题:
function Constructor() {
var privateProperty = 'private';
var privateMethod = function(){
alert('called from public method');
};
return {
publicProperty: 'im public',
publicMethod: function(){
alert('called from public method');
},
getter: privateMethod
}
}
var myObj = new Constructor();
//public
var pubProp = myObj.publicProperty;
myObj.publicMethod();
myObj.getter();
//private - will cause errors
myObj.privateProperty
myObj.privateMethod
Run Code Online (Sandbox Code Playgroud)
一位用户评论我的回答说:
此外,如果您的函数显式返回一个对象,那么使用new调用它并不是一个好习惯,因为这会产生误导 - 如果使用new,您希望结果是Constructor的一个实例
我通常使用new创建对象.但为什么这不是一个好的做法?似乎使用new而不使用new返回相同的东西.从闭包创建对象的正确方法是什么?
我知道这已被问过几百次,但是,我似乎无法掌握这个概念 prototype
这是我的示例脚本
var config = {
writable: true,
enumerable: true,
configurable: true
};
var defineProperty = function(obj, name, value) {
config.value = value;
Object.defineProperty(obj, name, config);
}
var man= Object.create(null);
defineProperty(man, 'sex', "male");
var person = Object.create(man);
person.greet = function (person) {
return this.name + ': Why, hello there, ' + person + '.'
}
var p=Object.getPrototypeOf(person);
alert(p.sex);//shows male
person.prototype.age=13;//why there is a error said the prototype is undefined? I thought it supposed be man object...
var child=function(){}
child.prototype.color="red";//why …Run Code Online (Sandbox Code Playgroud) javascript ×8
oop ×2
prototype ×2
c# ×1
cesium ×1
closures ×1
drupal ×1
drupal-7 ×1
drupal-8 ×1
inheritance ×1
new-operator ×1
syntax ×1