如何在angularjs中使用OOP类

时间:2023-01-17 00:07:31

I need to know how I can use a class in angularjs.

我需要知道如何在angularjs中使用类。

A simple class like this:

这样一个简单的类:

function Person(name,age){
    this.name=name;
    this.age=age;
    this.getAge(){
       return age;
    };
    //etcetera
}

I am learning angular but I can't find an answer to this. I want to create Person instances and use the name, age etc. in the view.

我正在学习角度但我无法找到答案。我想创建Person实例并在视图中使用名称,年龄等。

Please a simple solution because I am from a C# OOP background and don't know js frameworks. But I want to use the MVC pattern. Thanks!

请一个简单的解决方案,因为我来自C#OOP背景,不知道js框架。但我想使用MVC模式。谢谢!

2 个解决方案

#1


1  

AngularJS is a framework that is written in and used through JavaScript. Although JavaScript has many object oriented features, it handles inheritance quite differently. This used to be confusing to me too until I read the excellent: JavaScript: The Good Parts by Douglas Crockford. I suggest you read this, it's quite a brief book and points out differences in approach in a very clear and concise manner.

AngularJS是一个用JavaScript编写和使用的框架。尽管JavaScript具有许多面向对象的功能,但它处理的继承却截然不同。在我阅读优秀文章之前,这一直让我感到困惑:道格拉斯克罗克福德的JavaScript:The Good Parts。我建议你阅读这篇文章,这是一本非常简短的书,并以非常简洁明了的方式指出方法上的差异。

#2


1  

Your syntax is almost right, you just need to change the function declaration a bit:

你的语法几乎是正确的,你只需要稍微更改一下函数声明:

function Person(name, age) {
    this.name = name;
    this.age = age;

    this.getAge = function () {

        return age;
    };
}

Then you can create an instance like this:

然后你可以创建一个像这样的实例:

var p = new Person("John", 26);

var p = new Person(“John”,26);

And do a call like this:

并做这样的电话:

console.log(p.name + ' is ' + p.getAge() + ' years old');

console.log(p.name +'是'+ p.getAge()+'岁');

Here it is in JS Fiddle.

这是JS Fiddle。

In general, I highly recommend using Typescript, it complements angularjs in an excellent way. It makes your project very scalable, and adds intellisense. You can declare interfaces, classes, etc.

一般来说,我强烈建议使用Typescript,它以极好的方式补充angularjs。它使您的项目具有可扩展性,并增加了智能感知功能。您可以声明接口,类等。

#1


1  

AngularJS is a framework that is written in and used through JavaScript. Although JavaScript has many object oriented features, it handles inheritance quite differently. This used to be confusing to me too until I read the excellent: JavaScript: The Good Parts by Douglas Crockford. I suggest you read this, it's quite a brief book and points out differences in approach in a very clear and concise manner.

AngularJS是一个用JavaScript编写和使用的框架。尽管JavaScript具有许多面向对象的功能,但它处理的继承却截然不同。在我阅读优秀文章之前,这一直让我感到困惑:道格拉斯克罗克福德的JavaScript:The Good Parts。我建议你阅读这篇文章,这是一本非常简短的书,并以非常简洁明了的方式指出方法上的差异。

#2


1  

Your syntax is almost right, you just need to change the function declaration a bit:

你的语法几乎是正确的,你只需要稍微更改一下函数声明:

function Person(name, age) {
    this.name = name;
    this.age = age;

    this.getAge = function () {

        return age;
    };
}

Then you can create an instance like this:

然后你可以创建一个像这样的实例:

var p = new Person("John", 26);

var p = new Person(“John”,26);

And do a call like this:

并做这样的电话:

console.log(p.name + ' is ' + p.getAge() + ' years old');

console.log(p.name +'是'+ p.getAge()+'岁');

Here it is in JS Fiddle.

这是JS Fiddle。

In general, I highly recommend using Typescript, it complements angularjs in an excellent way. It makes your project very scalable, and adds intellisense. You can declare interfaces, classes, etc.

一般来说,我强烈建议使用Typescript,它以极好的方式补充angularjs。它使您的项目具有可扩展性,并增加了智能感知功能。您可以声明接口,类等。