我的代码如下.
var Main = function () {
var a, b, c, d;
a = 1;
b = true;
c = undefined;
var _private = function () {
return 'Function with Private acceess';
};
this.getPublic = function () {
return 'Function with Public access';
};
this.getPrivate = function () {
_private();
};
};
var o = new Main();
console.log(o.getPublic());
console.log(o.getPrivate());
Run Code Online (Sandbox Code Playgroud)
在上面的代码中,我试图通过public方法访问Main对象o的私有方法getPrivate().但在控制台中的结果是
undefined
Run Code Online (Sandbox Code Playgroud)
为什么_private没有返回所需的值?
function Developer(skill) {
this.skill = skill;
this.says = function() {
alert(this.skill + ' rocks!');
}
}
var john = new Developer('Ruby');
var func = john.says;
func();
Run Code Online (Sandbox Code Playgroud)
我尝试这个例子时,我得到以下消息,undefined rocks! 而不是Ruby rocks!.你能解释一下为什么会这样吗?
我试图使用Lo-Dash javascript库在javascript中模拟继承.
我只是使用_.extend这样做:
function Parent() {
var self = this;
self.hello = function () {
console.log('Hello from parent');
};
}
function Child() {
var self = this;
_.extend(self, Parent);
}
Child.hello(); // Doesn't exist
Run Code Online (Sandbox Code Playgroud)
我认为这可行,因为所有的javascript函数都是对象,但显然我错了.为什么这不起作用,我将如何使用Lo-Dash正确模拟继承?
我需要将对类构造函数的引用存储在变量中,并稍后构造该对象。这是我的最小代码示例:
function A() {
var _foo = "A";
}
function Wrapper( constructorFunc ) {
var _constructorFunc = constructorFunc;
this.constructorFunc = function() {
return _constructorFunc;
}
}
var wrapper = new Wrapper( A.constructor );
var cFunc = wrapper.constructorFunc();
var obj = new cFunc(); /* obj should be an A now */
Run Code Online (Sandbox Code Playgroud)
我希望我想做什么很清楚。Firebug 控制台给出错误TypeError: cFunc is not a constructor。正确的方法是什么?
此外,我必须能够“比较”构造函数,即我需要知道两个引用是否指向同一个构造函数。(在 C++ 中这是可能的,因为比较函数的地址。)举个例子:
function A() {
var _foo = "A";
}
function B() {
var _bar = "B";
}
function Wrapper( …Run Code Online (Sandbox Code Playgroud) 我的JS代码:
console.clear();
function BaseClass(nname) {
var name = nname;
this.bc_PublicProperty = "DefaultValue_BaseClass";
this.bc_getName = function GetName() {
return name;
};
this.bc_setName = function SetName(nname) {
name = nname;
};
}
function SubClass(nname) {
BaseClass.call(this, nname);
this.sc_PublicProperty = "DefaultValue_SubClass";
this.sc_getName = function GetName() {
return name;
};
this.sc_setName = function SetName(nname) {
name = nname;
};
}
SubClass.prototype = Object.create(BaseClass.prototype);
SubClass.prototype.constructor = SubClass;
var bc = new BaseClass("Anton");
var sc = new SubClass("Bernd");
console.log("hasOwnProperty check on subclass object 'sc' returns true …Run Code Online (Sandbox Code Playgroud)javascript coding-efficiency prototypal-inheritance hasownproperty
我有一个基类
function Base(){
this.children = new Array();
}
Base.prototype.Add = function(child){
this.children.push(child)
}
function Sub1(){...}
Sub1.prototype = new Base();
function Sub2(){...}
Sub2.prototype = new Base();
Run Code Online (Sandbox Code Playgroud)
那我该怎么办呢
var S1_1 = new Sub1();
var S1_2 = new Sub1();
S1_1.Add(new Sub2());
Run Code Online (Sandbox Code Playgroud)
S1_2由于某种原因也有1个孩子并且包含与我添加到S1_1的孩子相同的信息?
为什么"numMyNumber"没有出现在Object.getOwnPropertyNames中?
在Firefox中使用FireBug控制台.
"use strict";
// MyFunction
function MyFunction() {
var numMyNumber = 10;
return numMyNumber;
}
// ["prototype", "length", "name", "arguments", "caller"]
// Why does numMyNumber not appear?
console.log(Object.getOwnPropertyNames (MyFunction));
// 10
console.log(MyFunction());
Run Code Online (Sandbox Code Playgroud) 有最流行的JavaScript继承方法:
function extend(Child, Parent) {
var F = function() { }
F.prototype = Parent.prototype;
Child.prototype = new F();
Child.prototype.constructor = Child;
Child.superclass = Parent.prototype;
}
Run Code Online (Sandbox Code Playgroud)
我现在正在学习JS,但是我有很多Java经验,这种继承模型对我来说似乎有些困难.为什么我不能做以下事情:
function extend(Child, Parent) {
Child.prototype = Parent.prototype;
Child.prototype.constructor = Child;
}
Run Code Online (Sandbox Code Playgroud)
我想我不了解/了解一些事情,但在我看来,F对象毫无用处.请澄清一下情况.谢谢.
如何检查原型函数是否存在?
再解释一下:
正如您在示例代码中看到的那样,我将始终拥有commonFunction()for X1和X2.
我想告诉,如果X1和X2有自己的myOwnFunction().
重要的是要注意到,我不知道我将调用哪个函数.这就是为什么我需要一种动态的方式来收集这些信息.
代码:
function FunctionMain (){};
FunctionMain.FunctionSub = new FunctionSub();
function FunctionX1()
{
FunctionX1.prototype.commonFunction = function()
{
console.log("Hello, I'm X1");
}
FunctionX1.prototype.myOwnFunctionX1 = function()
{
console.log("This my own function");
}
}
function FunctionX2()
{
FunctionX2.prototype.commonFunction = function()
{
console.log("Hello, I'm X2");
}
//I don't have myOwnFunctionX2()
}
function FunctionSub()
{
FunctionSub.prototype.FunctionX1 = new FunctionX1();
FunctionSub.prototype.FunctionX2 = new FunctionX2();
}
//This call works!
FunctionMain.FunctionSub.FunctionX1.commonFunction();
FunctionMain.FunctionSub.FunctionX2.commonFunction(); …Run Code Online (Sandbox Code Playgroud) 我在原型之间感到困惑.我已经研究并观看了教程,但我仍未能得到明确的答案.很高兴,如果有人可以帮助我.如果使用一些简单的例子或解释很好.
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点.
谢谢.
javascript ×10
constructor ×1
factory ×1
function ×1
inheritance ×1
lodash ×1
oop ×1
prototype ×1
prototypejs ×1