相关疑难解决方法(0)

究竟是什么Javascript原型?一个库或内置的Javascript?

我在原型之间感到困惑.我已经研究并观看了教程,但我仍未能得到明确的答案.很高兴,如果有人可以帮助我.如果使用一些简单的例子或解释很好.

  1. Prototype是库吗?例如Jquery

    如是.这意味着我们需要在使用它之前将其添加到我们的文件中.就像我们将Jquery添加到头部,然后我们可以访问它的所有功能.

    因此我们需要在使用它之前学习它,因为原型是使用像Jquery这样的纯javascript构建的.

  2. 如果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'; };

  3. 有人说Prototype实际上是一个Javascript.

    如果这是正确的,那么我们如何在JSFiddle下面的列表中分离原型和jQuery. 在此输入图像描述

  4. 或者上面的图像中的Prototype库是否与Javascript原型对象不同?

    意味着这些是两件不同的事情.

能否请你澄清我的这4点.

谢谢.

javascript prototypejs

1
推荐指数
1
解决办法
3750
查看次数

修改对象的原型不起作用

我一直试图找出为什么这不起作用.如果有人可以帮助我,我将不胜感激!

function Person(name, age) {
    this.name = name;
    this.age = age;
    var ageInTenYears = age + 10; 
    this.sayNameAndAge = function() {
      console.log(name + age);
    }
}

Person.prototype.sayAge = function() {
   console.log(this.age); 
}

Person.prototype = { 
    sayName : function(){
       console.log(this.name); 
    },
    sayNameAfterTimeOut : function(time) {
       setTimeout(this.sayName, time);
    },
    sayAgeInTenYears : function() { 
       console.log(ageInTenYears);
    } 
}

var bob = new Person('bob', 30); 
bob.sayName();
Run Code Online (Sandbox Code Playgroud)

我收到此错误:

  Uncaught TypeError: Object #<Object> has no method 'sayAge' 
Run Code Online (Sandbox Code Playgroud)

javascript prototype prototype-programming

1
推荐指数
1
解决办法
73
查看次数

封装在JavaScript中,它是否存在?

我有使用C#编程语言的经验,但我现在也必须使用JS,这对我来说相当新.

我试图在JS中开发一个简单的类仿真,如下所示:

http://jsfiddle.net/T74Zm/

function A( inputValue ) {
    this.Init( inputValue );
    this.Print();
}
A.prototype = {
    value: null,
    Init: function( inputValue ) {
        this.value = inputValue;
    },
    Print: function () {
        console.log( this.value );
    }
}

var obj = new A(40);
Run Code Online (Sandbox Code Playgroud)

我试图封装变量valueA.prototype,但它似乎是JavaScript的规范,所有的对象都是可用的.

所以我的问题是:

1).如何使用OOP /访问修饰符支持非常接近静态语言的封装?

2).如何在JS中模拟一些访问修饰符,private例如?

javascript oop encapsulation prototype object

1
推荐指数
2
解决办法
1313
查看次数

在JavaScript中编写对象方法的最佳方法是什么?

OOP建议只公开您希望用户能够访问的变量和方法.我一直在为我的对象使用公共方法声明(即原型).

DrawShape = function() {

}

DrawShape.prototype.circle = function() {
    // draw logic here
}

DrawShape.prototype.square = function() {
    // draw logic here
}
Run Code Online (Sandbox Code Playgroud)

这种方法似乎是最有效的,因为每次实例化实例时都不会重写该方法.但是我发现要创建好的DRY,模块化代码我必须创建仅供其他方法访问的方法(即私有方法).

DrawShape = function() {

}

DrawShape.prototype.circle = function() {
    var colour = this.setColour();
    // draw logic here
}

DrawShape.prototype.square = function() {
    var colour = this.setColour();
    // draw logic here
}

DrawShape.prototype.setColour = function() {
    return "blue";
}
Run Code Online (Sandbox Code Playgroud)

在这里,我创建了一个名为setColour的方法,该方法仅用于由其他方法运行.问题是该方法是公开的,可以被任何人或任何人调用.

我可以将方法移动到对象构造函数中...但这意味着我不再保存内存(即每次实例化实例时都会重写),这也意味着我必须移动所有其他方法进入构造函数.

在创建对象时,JavaScript中的最佳实践是什么?

javascript prototype object

1
推荐指数
1
解决办法
374
查看次数

使用下划线继承

我上过课 -

Zoo.Controller = (function() {

  function Controller() {}

  Controller.prototype.params = {};

  Controller.prototype.set_params = function(params) {
    this.params = params;
    return this;
  };

  return Controller;

})();
Run Code Online (Sandbox Code Playgroud)

我想使用_.extend从该类继承

Zoo.Controllers.WhaleController = _.extend({

  new: function () {
    // do something
  }

}, Zoo.Controller);
Run Code Online (Sandbox Code Playgroud)

当我尝试像这样实例化那个类时......

this.whale_controller = new Zoo.Controllers.WhaleController();
Run Code Online (Sandbox Code Playgroud)

我明白了 -

Uncaught TypeError: object is not a function
Run Code Online (Sandbox Code Playgroud)

有可能做我正在尝试的事情吗?我在JS中读过很多关于继承的文章,但是假设Underscore库已经为我解决了.

javascript inheritance underscore.js

1
推荐指数
1
解决办法
8816
查看次数

JavaScript".method"返回undefined

以下脚本:

    <script>
        function Parenizor (value){
            this.setValue(value);
        }

        Parenizor.method('setValue', function (value){
            this.value = value;
            return this;
        });

        Parenizor.method('getValue', function (){
            return this.value;
        });

        Parenizor.method('toString', function(){
            return '(' + this.getValue() + ')';
        });

        myParenizor = new Parenizor(0);
        myString = myParenizor.toString();

        console.log(myParenizor, myString);
    </script>
Run Code Online (Sandbox Code Playgroud)

在控制台中我可以看到:"undefined不是一个函数"指的是:

Parenizor.method('setValue', function (value){
Run Code Online (Sandbox Code Playgroud)

我错过了什么吗?

javascript inheritance

1
推荐指数
1
解决办法
153
查看次数

someFunction.Prototype.constructor vs someFunction.constructor

我知道JavaScript中的函数首先导致函数的双重生命(作为创建实例的第一类)和普通对象的第二个函数.

但我很惊讶看到以下控制台的输出.

function A() {
    console.info("A");
}
console.info(A.prototype.constructor === A.constructor); // false
Run Code Online (Sandbox Code Playgroud)

我期望它是真实的,因为我不期望constructor对象的属性,A因为它是自己的属性.因此,在原型链查找后,它应该是同一个对象A.prototype.constructor.我错在哪里或者我遗失了什么?

javascript constructor prototype

1
推荐指数
1
解决办法
46
查看次数

在js中使用自调用函数的问题

<script>
var person = function () {
    // Private
    var name = "David";
    return {
        getName : function () {
            return name;
        },
        setName : function (newName) {
            name = newName;
        }
    };
}();
console.log(person.name);  
</script>
Run Code Online (Sandbox Code Playgroud)

题:

为什么它显示:在控制台中未定义?

javascript

0
推荐指数
1
解决办法
68
查看次数

在javascript中的类定义中添加原型

阅读可以使用哪些技术在JavaScript中定义一个类,以及它们的权衡取舍是什么?在stackoverflow上我明白我可以通过定义一个类

方法1:

function Person(name, gender){
   this.name = name;
   this.gender = gender;
}
Run Code Online (Sandbox Code Playgroud)

并在原型中添加函数,以避免每次实例化时重新创建成员函数.喜欢

Person.prototype.speak = function(){
   alert("my name is" + this.name);
}
Run Code Online (Sandbox Code Playgroud)

并通过创建其实例

var person = new Person("Bob", "M"); 
Run Code Online (Sandbox Code Playgroud)

我认为使用新的关键字就可以创建相同的对象

方法2:

var Person = function (name, gender) {

    return {name:name, gender:gender}; 

}

person  = Person("Bob", "M");
Run Code Online (Sandbox Code Playgroud)

第二种方法是否完成了第一种方法完成相同的操作?如果是这样的话,我将如何在第二种方法中通过原型模拟添加函数(正如我们在方法1中所说的那样)

javascript prototype class object

0
推荐指数
1
解决办法
146
查看次数

原型实例如cat c = new animal();?

我是Javascript的新手,最近,我遇到了这个问题,我很想知道答案.

function animal(){
  // some bse code
}


function cat(){
  //some cat code
}

// I know this syntax and works well
cat.prototype= new animal;
Run Code Online (Sandbox Code Playgroud)

我想知道下面的语法是否正确?

cat c = new animal;
Run Code Online (Sandbox Code Playgroud)

在javascript中有可能吗?

(对不起!如果问题存在.)

javascript oop jquery prototype

0
推荐指数
1
解决办法
247
查看次数