JavaScript 中的 init() 用法有什么用?

发布于 2024-12-11 17:31:48 字数 52 浏览 2 评论 0原文

JavaScript 中 init() 函数的含义和用法是什么?

What is the meaning and usage of the init() function in JavaScript?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

杀手六號 2024-12-18 17:31:48

JavaScript 没有内置的 init() 函数,也就是说,它不是该语言的一部分。但是,个体程序员创建自己的 init() 函数来进行初始化的情况并不罕见(在很多语言中)。

特定的 init() 函数可用于初始化整个网页,在这种情况下,它可能会从 document.ready 或 onload 处理中调用,或者可能会初始化特定类型的对象,或者……好吧,你说吧。

任何给定的 init() 具体执行的操作实际上取决于编写它的人需要它执行的操作。某些类型的代码不需要任何初始化。

function init() {
  // initialisation stuff here
}

// elsewhere in code
init();

JavaScript doesn't have a built-in init() function, that is, it's not a part of the language. But it's not uncommon (in a lot of languages) for individual programmers to create their own init() function for initialisation stuff.

A particular init() function may be used to initialise the whole webpage, in which case it would probably be called from document.ready or onload processing, or it may be to initialise a particular type of object, or...well, you name it.

What any given init() does specifically is really up to whatever the person who wrote it needed it to do. Some types of code don't need any initialisation.

function init() {
  // initialisation stuff here
}

// elsewhere in code
init();
凡间太子 2024-12-18 17:31:48

在 JavaScript 中,当您通过构造函数调用创建任何对象时,如下所示

步骤 1:创建一个名为 Person 的函数..

function Person(name){
this.name=name;
}
person.prototype.print=function(){
console.log(this.name);
}

步骤 2:为此函数创建一个实例..

var obj=new Person('venkat')

//上面的行将实例化此函数(Person)并返回一个全新的名为 Person {name:'venkat'} 的对象,

如果您不想实例化此函数并同时调用。我们也可以像下面这样做。

var Person = {
  init: function(name){
    this.name=name;
  },
  print: function(){
    console.log(this.name);
  }
};
var obj=Object.create(Person);
obj.init('venkat');
obj.print();

在上面的方法中 init 将帮助实例化对象属性。基本上 init 就像类上的构造函数调用。

In JavaScript when you create any object through a constructor call like below

step 1 : create a function say Person..

function Person(name){
this.name=name;
}
person.prototype.print=function(){
console.log(this.name);
}

step 2 : create an instance for this function..

var obj=new Person('venkat')

//above line will instantiate this function(Person) and return a brand new object called Person {name:'venkat'}

if you don't want to instantiate this function and call at same time.we can also do like below..

var Person = {
  init: function(name){
    this.name=name;
  },
  print: function(){
    console.log(this.name);
  }
};
var obj=Object.create(Person);
obj.init('venkat');
obj.print();

in the above method init will help in instantiating the object properties. basically init is like a constructor call on your class.

西瓜 2024-12-18 17:31:48

注意。构造函数名称应以大写字母开头,以区别于普通函数,例如 MyClass 而不是 myClass

您可以从构造函数中调用 init

var myObj = new MyClass(2, true);

function MyClass(v1, v2) 
{
    // ...

    // pub methods
    this.init = function() {
        // do some stuff        
    };

    // ...

    this.init(); // <------------ added this
}

或者更简单地说,您可以将 init 函数的主体复制到构造函数的末尾。如果仅调用一次,则实际上根本不需要 init 函数。

NB. Constructor function names should start with a capital letter to distinguish them from ordinary functions, e.g. MyClass instead of myClass.

Either you can call init from your constructor function:

var myObj = new MyClass(2, true);

function MyClass(v1, v2) 
{
    // ...

    // pub methods
    this.init = function() {
        // do some stuff        
    };

    // ...

    this.init(); // <------------ added this
}

Or more simply you could just copy the body of the init function to the end of the constructor function. No need to actually have an init function at all if it's only called once.

毁梦 2024-12-18 17:31:48

这更像是无法访问的代码

,例如。如果变量 x 或函数 x() 在其所在行下方声明
调用此错误会生成:

 setPlayerOne();

let imageGenerator = (value) =>{
allImages = {
    1: 'images/dice1.png',
    2: 'images/dice2.png',
    3: 'images/dice3.png',
    4: 'images/dice4.png',
    5: 'images/dice5.png',
    6: 'images/dice6.png',
}
  return allImages[Number(value)];
}

findRandom = () =>{
    let randomNumber = Math.floor(Math.random() * 6) +1
    return randomNumber;
}

let setPlayerOne = () =>{
   let img1 = document.querySelector('.img1').attributes.src;
   img1.value = imageGenerator(findRandom())
}

let setPlayerTwo = () =>{
    let img2 = document.querySelector('.img2').attributes.src;
    img2.value = imageGenerator(findRandom())
}

  
setPlayerTwo();

setPlayerOne() 方法将生成此错误,但 setPlayerTwo() 不会生成;
这是因为 setPlayerOne() 在 JS 初始化之前被调用。

This is more like unreachable code

eg. if variable x or function x() is declared below the line where its
called this error generates:

 setPlayerOne();

let imageGenerator = (value) =>{
allImages = {
    1: 'images/dice1.png',
    2: 'images/dice2.png',
    3: 'images/dice3.png',
    4: 'images/dice4.png',
    5: 'images/dice5.png',
    6: 'images/dice6.png',
}
  return allImages[Number(value)];
}

findRandom = () =>{
    let randomNumber = Math.floor(Math.random() * 6) +1
    return randomNumber;
}

let setPlayerOne = () =>{
   let img1 = document.querySelector('.img1').attributes.src;
   img1.value = imageGenerator(findRandom())
}

let setPlayerTwo = () =>{
    let img2 = document.querySelector('.img2').attributes.src;
    img2.value = imageGenerator(findRandom())
}

  
setPlayerTwo();

setPlayerOne() method will generate this but setPlayerTwo() will not generate;
this because setPlayerOne() was called before initialized by JS.

迷路的信 2024-12-18 17:31:48

JavaScript 总是必须从对象创建原型,因此我们使用 init() 函数创建一个对象,以便使用一些预定义的信息初始化引用的对象。

Object.create() 没有构造函数。最常见的模式是必须手动创建的 init 函数或构造函数:

const userTest = {
    init: function (name, id, email){
        this.name = name;
        this.id = id;
        this.email = email;
    },
    result: function(){
    return `${this.name}, ${this.id}, ${this.email}`;
    }
}


const userTestDavid = Object.create(userTest);
userTest.init('David, 33333432109, [email protected]');

console.log(userTest.result());

JavaScript always has to create the prototype from an object, so we create an object with a init() function in order to initialize the referred object with some pre-defined information.

Object.create() does not have a constructor. The most common pattern is an init function or constructor that must be created manually:

const userTest = {
    init: function (name, id, email){
        this.name = name;
        this.id = id;
        this.email = email;
    },
    result: function(){
    return `${this.name}, ${this.id}, ${this.email}`;
    }
}


const userTestDavid = Object.create(userTest);
userTest.init('David, 33333432109, [email protected]');

console.log(userTest.result());
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文